Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

perl

How to write comments and simple statements in Perl

Khizar Hayat Saani

When coding, it is always important to write comments that explain your code. Comments make your code easier to read and understand.

Most coding languages have a specific syntax that accommodates both line and block comments.

Let’s take a closer look at how comments and simple statements work in Perl.

Important symbols for comments and statements

Simple statements

Statements are instructions for the compiler. They perform operations on variables during run-time.

All statements must end with a semi-colon (;) in Perl. This aspect of programming in Perl is similar to C/C++ or Java.

Statements in Perl can be single-line or multi-line. Multi-line statements are handy when writing an expression that is too long for one line.

Let’s write a few statements that perform basic arithmetic operations:

$v = 4;
$w = 7;
$x = 10;
$y = 2;

$z = $w + $y *
     $x - $v;

Comments

Comments are parts of your code that the compiler ignores during compile-time. They merely serve the purpose of conveying information to the reader.

Like statements, comments in Perl can also be single line and multi-line. Multi-line comments are often referred to as ‘block comments.’

Single line comments start with the # symbol.

## This is a single-line comment

Block comments in Perl are enclosed within the = and =cut symbols. Everything written after the = symbol is considered part of the comment until =cut is encountered.

There should be no whitespace following = in the multi-line comment.

=This is
a
block comment 
=cut

Code

We will now write a simple program in Perl that shows how to write comments and simple statements.

=This program will
display 'Hello World' on the
screen
=cut

$string = "Hello World"; # A simple assignment statement
print $string;

RELATED TAGS

perl

CONTRIBUTOR

Khizar Hayat Saani
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring