Perl Programming   «Prev 

Perl Comments

For the general syntax and structure of Perl, unlike statements, comments are line-oriented.
Anything following a # character, up to the end of the line, is considered a comment and is ignored:

 $x = 1;# this is a comment
 print "$x\n"; # so is this!

Single Line Comments

Perl currently allows for single-line comments using the pound-symbol "#".
Any implementation of a Perl multilline comment should feel similar to this.
However (ideally) the multiline syntax would be unique enough so that it would not conflict with older scripts using the singleline "#" syntax.

Perl Comments created using hash Symbol

Comments are treated by Perl as white space and the moment Perl sees a hash on a line outside of a quoted block, the remainder of the line is ignored. This is the case even within multiline statements and regular expressions (when the /x modifier is used):
Perl Space Separator
Perl Space Separator

Comments end when Perl sees a normal line termination. The following is completely invalid:
print("Hello world"); # Greet the user
and let them know we're here

Multiline comment

There is also no way of indicating to Perl that you have a multiline comment to include, short of placing the hash symbol before each comment segment. If the comment includes a "line directive"; in this instance the information is stored within the opcode tree and used to populate the __LINE__ and __FILE__ special tokens. These are available directly and are also used as the basis for error messages raised by
  1. die and
  2. warn
when no trailing newline is supplied. In order to introduce the directive, you must use the word line, followed by a line number and an optional string. The match is actually made by a regular expression:

/^#\s*line\s+(\d+)\s*(?:\s"([^"]+)?\s*$/
The first group, $1 populates __LINE__, and $2 populates __FILE__. For example:
# line 200 "Parsing engine"
die "Fatal";
produces
Fatal at Parsing engine line 200

Note that the line directive actually modifies the __LINE__ token, which is normally automatically parsed and populated by the Perl interpreter based on the current line within the script. So this script:
#line 200 "Parsing engine"
print "Busy\n";
print "Doing\n";
print "Nothing\n";
die 'Fatal';

actually reports this:
Busy
Doing
Nothing
Fatal at Parsing engine line 203.

It reported an error on line 203, not the real source line 4, the earlier line directive has permanently modified the line-numbering counters. You can update the line directive with any number, such that
#line 200 "Parsing engine"
print "Busy doing nothing\n";
warn "Warning";
#line 100 "Post-process engine"
print "Working the whole day through\n";
die "Fatal";

generates this:
Busy doing nothing
Warning at Parsing engine line 201.
Working the whole day through
Fatal at Post-process engine line 101.

Comments and line directives can be a useful way of debugging and documenting your scripts and programs.