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.
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;
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)