Perl Tutorial Perl References

Perl - Comments



The Comments are added in programming code with the purpose of making the code easier to understand. It makes the code more readable and hence easier to update the code later. Comments are ignored by the compiler while running the code. In Perl, there are two ways of putting a comment.

  • Single line comment
  • Multi-line comment

Single line Comment

It starts with # and ends with the end of that line. Anything after # to the end of the line is a single line comment and will be ignored by the compiler.

Example:

The example below illustrates how to use single line comments in a Perl code. The compiler ignores the comments while compiling the code.

# first line comment
print("Hello World!"); # second line comment

The output of the above code will be:

Hello World!

Multi-line Comment

Perl uses a special markup language POD (it stands for Plain Old Documentation). It starts with = followed by no space is Directive (for example - =a, =begin or =head2) and ends with =cut. Anything between =a and =cut is a multi-line comment and will be ignored by compiler.

Syntax

=a
comment lines
=cut

Example:

The example below describes how to use multiple line comments in a Perl code which is ignored by the compiler while compiling the code.

=begin
comment line 1
comment line 2
=cut

print("Hello World!\n");

=new multi-line comment
comment line 3
comment line 4
=cut 

print("Programming is fun") 

The output of the above code will be:

Hello World!
Programming is fun