Introduction
Perl is the interpreted language Larry Wall released in 1987, known for its text handling and regular expressions. Perl 5, first released in 1994, is the language still in use. The project once called Perl 6 became a separate language, Raku, with its own extensions.
A script is usually named .pl and a module .pm. The
.pl extension is also used by Prolog, most visibly by SWI-Prolog, so a
.pl file can be either language; Prolog source is built from facts and
rules ending in full stops. This site tells them apart from the text: a .pl
file that passes a strict Prolog test (a directive such as :- module(, a
grammar rule with -->, or clauses that all read as Prolog terms, and no mark of
Perl such as use strict; or my $x) opens as
Prolog source, with its own pages
(how the two are told apart). Anything
ambiguous, and every file with a mark of Perl, stays Perl.
Structure
A Perl file is plain text. A script often starts with a shebang line such as
#!/usr/bin/perl, then use strict; and
use warnings;. Statements end with semicolons and blocks use braces.
Comments start with #.
Variables carry a sigil that shows their kind: $ for a scalar,
@ for an array and % for a hash. Regular expressions are part
of the syntax, as in $text =~ /pattern/.
A module is a package whose name matches its path, so
Foo::Bar is in Foo/Bar.pm. By tradition it ends with
1;, because loading a module needs a true value. Documentation is written
in POD, blocks that start at a line such as =head1 NAME and end at
=cut. __END__ marks the end of the code.
#!/usr/bin/perl
use strict;
use warnings;
# Count the words on standard input.
my %count;
while (my $line = <STDIN>) {
$count{$_}++ for split ' ', lc $line;
}
print "$_: $count{$_}\n" for sort keys %count;
Not described
Known, not yet covered
- Reference: this is a single overview page with no language reference under it. The grammar and its parts are not described: contexts (scalar and list), the special variables (
$_,@ARGV,%ENV,$!), references and data structures, the quote-like operators and regular expression syntax, subroutines and signatures, packages,blessand object-oriented Perl,useand the standard pragmas, and the built-in functions. perlsyn, perlop, perlfunc and perlre in the official documentation describe them. - Reference: POD is named but its commands and formatting codes are not listed (see perlpod), and the Perl versions, from 5.8 to the 5.40 series, are not described: what each added (
say,given, signatures, the postfix dereference) or removed. - Reference: the other Perl file types (
.pod,.ttest scripts,.xsextension code,Makefile.PL,cpanfile) are not described, and Raku has no page. Prolog files that use.plhave their own Prolog pages. - Viewer: whether a
/divides or starts a pattern is a guess from the token before it. After a variable, a number, a string, a closing bracket, a subscript ($h{k} / 2) or a function that takes no argument (time,times,wait,wantarray,fork) it divides, and//and//=are the defined-or operators. After any other built-in function (split /,/) or an operator it starts a pattern, and a word that is not a Perl function is taken for a term, sofoo / 2divides. - Viewer: variables,
@{[ … ]}blocks and${\ … }inside a double-quoted string or heredoc are not coloured as code, class and package names are plain, and everything after__END__or__DATA__is coloured as a comment although it is data. - Viewer: the file is not parsed or run, so there is no outline, no
use strictchecking and no list of problems, and only the first 1 MiB is read and coloured.
Also used for
- Prolog source – Prolog programs as text; the extension is also used for Perl, so the text decides.
Links
Software
- Perl – the interpreter, included with most Linux and Unix systems (Windows, macOS, Linux)
- Strawberry Perl – Perl distribution for Windows with a compiler for modules (Windows)
- SWI-Prolog – Prolog system whose source files use
.pl(Windows, macOS, Linux) - Visual Studio Code – free code editor with Perl extensions (Windows, macOS, Linux)
Developers
- CPAN – the main repository of Perl modules
- PPI – parses Perl source without running it (Perl)
- Perl::Critic – checks Perl code against style rules (Perl)
- Perl::Tidy – formats Perl source (Perl)