Perl Programming   «Prev  Next»
Lesson 8Perl general syntax and structure
ObjectiveBasic syntax and structure of Perl program

Perl General Syntax and Structure

Learn the basic syntax and structure of a Perl program. Perl is structured to allow as great a variety of syntactic constructions as possible.
It is not a line-oriented language. Perl statements are terminated by a semicolon (;) character. (The semicolon is optional at the end of a block, but we'll cover blocks later.)
So this:
$x = 1;
print "$x\n";

is syntactically equivalent to this:
$x = 1; 
print "$x\n";

There are two structural elements common to Perl scripts: 1) Comments and 2) Shebang

Perl 6

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):
    matched = /
        (\S+)      # Host
        \s+        # (space separator)
        (\S+)      # Identifier
        \s+        # (space separator)
        (\S+)      # Username
        \s+        # (space separator)
        \[(.*)\]   # Time
        \s+        # (space separator)
        "(.*)"     # Request
        \s+        # (space separator)
        (\S+)      # Result
        \s+        # (space separator)
        (\S+)      # Bytes sent
    /x;
    

    This is a regular expression pattern written in Perl using the `/x` modifier, which allows for whitespace and comments within the pattern for better readability. It appears to be designed to parse a log file or similar structured text input.
    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.


Perl Shebang line

If you have looked at Perl scripts, then you have seen that they most always start with
    
#!/usr/bin/perl
According to a Unix convention when the first two bytes of a file are #! (23H, 21H), whatever is in the remainder of that first line is taken to be the command line for the shell to run the script with. Therefore, it is customary to start every Perl program with a line like this:
#!/usr/bin/perl
  • Actual Path: Be sure to use the actual path to the perl program on your system (/usr/bin/perl is common for Perl 5).
    This is often called the shebang line because of what it sounds like when you say "hash-bang" for the millionth time.
  • The -w switch: You can put command-line switches in the shebang line, and they will be passed to the perl program as well.
    This command is going to occur frequently.
    #!/usr/bin/perl -w
    

    The -w switch tells perl to warn you about common misuses of variables and subroutines. It is useful, not only for beginners, but to catch the sorts of errors that we all make from time to time. Even on operating systems that do not use the shebang convention, this line is still useful in your Perl programs. The perl interpreter will use it anyway and apply whatever command-line switches you have included.

Perl is considered by some to have convoluted and confusing syntax; others consider this same syntax to be compact, flexible, and elegant. Though the language has most of the features one would expect in a full service programming notation, Perl has become well known for its capabilities in a few areas where it exceeds the capabilities of most other languages. These include
  1. String manipulation
  2. File handling
  3. Regular expressions and pattern matching
  4. Flexible arrays (hashes, or associative arrays)
In the next lesson, a test bed will be created in which you can experiment with the code you are going to encounter throughout this course.

SEMrush Software