TExcelTAD
Home › Perl › Running Perl

Running Perl

Perl is an interpreted language: you write a plain text file and the perl program reads, compiles and runs it in one step. There is no separate build stage.

Checking your installation

Most Linux distributions and macOS already ship with Perl. On Windows, install Strawberry Perl. Check the version from a terminal:

bash
$ perl -v
This is perl 5, version 42, subversion 0 (v5.42.0) built for x86_64-linux
...

Your first script

Save this as hello.pl and run it with perl hello.pl:

perl · try it yourself
#!/usr/bin/env perl
use strict;
use warnings;

print "Hello, world!\n";
print "Perl is ready.\n";
Hello, world!
Perl is ready.

Line by line:

Line Meaning
#!/usr/bin/env perl The shebang. On Unix, it lets you run the file directly as ./hello.pl after chmod +x hello.pl. Perl itself treats it as a comment.
use strict; Forces you to declare variables and forbids some unsafe constructs.
use warnings; Prints warnings about suspicious code, such as using an undefined value.
print "...\n"; Writes text to standard output. \n is a newline.

Tip: Start every script with use strict; and use warnings;. They catch typos and mistakes that would otherwise silently produce wrong results.

Why strict matters

Without strict, a misspelled variable name simply creates a new, empty variable. With strict, Perl refuses to run the program at all, and tells you where the problem is:

perl · try it yourself
use strict;
use warnings;

my $total = 10;
print "Total: $totl\n";
Global symbol "$totl" requires explicit package name (did you forget to declare "my $totl"?) at /app/output.s line 5.
Execution of /app/output.s aborted due to compilation errors.

The error is reported at compile time, before a single line runs. That is a big help in larger programs. (/app/output.s is simply the file name the online runner gives your code; on your machine you would see hello.pl.)

Warnings in action

warnings does not stop the program, but it reports questionable operations on standard error:

perl · try it yourself
use strict;
use warnings;

my $name;
print "Before\n";
my $greeting = "Hello, " . $name;
print "$greeting!\n";
Before
Hello, !
Use of uninitialized value $name in concatenation (.) or string at /app/output.s line 6.

The program still ran, but the warning tells you that $name never got a value.

Note: Warnings go to standard error, while print writes to standard output. The runner here collects the two streams separately and shows standard error last; in a terminal the warning would appear between the two lines of output.

Useful command-line switches

Command What it does
perl script.pl Run a script
perl -c script.pl Only check the syntax ("script.pl syntax OK")
perl -e 'print "hi\n"' Run code given on the command line
perl -w script.pl Turn on warnings globally
perl -v Show the version
perldoc -f print Read the documentation of a built-in function

perldoc is Perl's built-in manual. perldoc perlintro gives a quick tour of the language, and perldoc perlfunc lists every built-in function.

Using say

Typing \n everywhere gets tedious. The say function works like print but adds a newline automatically. Enable it with use feature 'say'; (or with use v5.10; and later version declarations):

perl · try it yourself
use strict;
use warnings;
use feature 'say';

say "Line one";
say "Line two";
print "Line three\n";
Line one
Line two
Line three

Test yourself

Which two pragmas should start almost every Perl script?

  1. use utf8; and use POSIX;
  2. use strict; and use warnings;
  3. use say; and use print;
  4. #!perl and exit;

What does perl -c script.pl do?

  1. Compiles the script to a binary
  2. Checks the syntax without running it
  3. Clears the screen
  4. Counts the lines

How does say differ from print?

  1. say prints to standard error
  2. say adds a newline at the end
  3. say cannot print variables
  4. There is no difference