Lably
Skip to content

Perl language structure

  • 28 min read
  • 6 chapters

Related on Lably

Through Input/output, String (computer science) and Comment (computer programming)

Basic syntax

In Perl, the minimal Hello World program may be written as follows:

<span id="mwGg"></span><span class="k" id="mwGw">print</span><span class="w" id="mwHA"> </span><span class="s" id="mwHQ">"Hello, World!\n"</span>

This prints the string Hello, World! and a newline, symbolically expressed by an n character whose interpretation is altered by the preceding escape character (a backslash). Since version 5.10, the new 'say' builtin produces the same effect even more simply:

<span id="mwLA"></span><span class="n" id="mwLQ">say</span><span class="w" id="mwLg"> </span><span class="s" id="mwLw">"Hello, World!"</span>

An entire Perl program may also be specified as a command-line parameter to Perl, so the same program can also be executed from the command line (example shown for Unix):

<span id="mwMw"></span><span class="nv" id="mwNA">$</span><span class="w" id="mwNQ"> </span><span class="nv" id="mwNg">perl</span><span class="w" id="mwNw"> </span><span class="o" id="mwOA">-</span><span class="n" id="mwOQ">e</span><span class="w" id="mwOg"> </span><span class="s" id="mwOw">'print "Hello, World!\n"'</span>

The canonical form of the program is slightly more verbose:

<span id="mwPw"></span><span class="ch" id="mwQA">#!/usr/bin/env perl</span> <span class="k" id="mwQQ">print</span><span class="w" id="mwQg"> </span><span class="s" id="mwQw">"Hello, World!\n"</span><span class="p" id="mwRA">;</span>

The hash mark character introduces a comment in Perl, which runs up to the end of the line of code and is ignored by the compiler (except on Windows). The comment used here is of a special kind: it's called the shebang line. This tells Unix-like operating systems to find the Perl interpreter, making it possible to invoke the program without explicitly mentioning perl. (Note that, on Microsoft Windows systems, Perl programs are typically invoked by associating the .pl extension with the Perl interpreter. In order to deal with such circumstances, perl detects the shebang line and parses it for switches.)

The second line in the canonical form includes a semicolon, which is used to separate statements in Perl. With only a single statement in a block or file, a separator is unnecessary, so it can be omitted from the minimal form of the program—or more generally from the final statement in any block or file. The canonical form includes it, because it is common to terminate every statement even when it is unnecessary to do so, as this makes editing easier: code can be added to, or moved away from, the end of a block or file without having to adjust semicolons.

Version 5.10 of Perl introduces a say function that implicitly appends a newline character to its output, making the minimal "Hello World" program even shorter:

<span id="mwVw"></span><span class="k" id="mwWA">use</span><span class="w" id="mwWQ"> </span><span class="mf" id="mwWg">5.010</span><span class="p" id="mwWw">;</span><span class="w" id="mwXA"> </span><span class="c1" id="mwXQ"># must be present to import the new 5.10 functions, notice that it is 5.010 not 5.10</span> <span class="n" id="mwXg">say</span><span class="w" id="mwXw"> </span><span class="s" id="mwYA">'Hello, World!'</span>

Sign in to keep reading

5 more chapters are free with a Lably account.

More in Computing and communication