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:
$ 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:
#!/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;anduse 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:
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:
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
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):
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?
use utf8;anduse POSIX;use strict;anduse warnings;use say;anduse print;#!perlandexit;
What does perl -c script.pl do?
- Compiles the script to a binary
- Checks the syntax without running it
- Clears the screen
- Counts the lines
How does say differ from print?
sayprints to standard errorsayadds a newline at the endsaycannot print variables- There is no difference