Stats

572 Contributors: 14 Wednesday, May 31, 2017
Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Rip Tutorial: riptutorial@gmail.com
Roadmap: roadmap

Getting started with Perl Language

Remarks

Perl is the camel of languages: useful, but not always beautiful. It has rather good documentation of its own which can be accessed using the perldoc command from your shell/command prompt. It's also available online at perldoc.perl.org.

Versions

VersionRelease NotesRelease Date
1.0001987-12-18
2.0001988-06-05
3.0001989-10-18
4.0001991-03-21
5.0001994-10-17
5.0011995-05-13
5.0021996-02-29
5.0031996-06-25
5.004perl5004delta1997-05-15
5.005perl5005delta1998-07-22
5.6.0perl56delta2000-03-22
5.8.0perl58delta2002-07-18
5.8.8perl581delta,
perl582delta,
perl583delta,
perl584delta,
perl585delta,
perl586delta,
perl587delta,
perl588delta
2006-02-01
5.10.0perl5100delta2007-12-18
5.12.0perl5120delta2010-04-12
5.14.0perl5140delta2011-05-14
5.16.0perl5160delta2012-05-20
5.18.0perl5180delta2013-05-18
5.20.0perl5200delta2014-05-27
5.22.0perl5220delta2015-06-01
5.24.0perl5240delta2016-05-09
5.26.0perl5260delta2017-05-30

Getting started with Perl

Perl tries to do what you mean:

print "Hello World\n";

The two tricky bits are the semicolon at the end of the line and the \n, which adds a newline (line feed). If you have a relatively new version of perl, you can use say instead of print to have the carriage return added automatically:

5.10.0
use feature 'say';
say "Hello World";

The say feature is also enabled automatically with a use v5.10 (or higher) declaration:

use v5.10;
say "Hello World";

It's pretty common to just use perl on the command line using the -e option:

$ perl -e 'print "Hello World\n"'
Hello World

Adding the -l option is one way to print newlines automatically:

$ perl -le 'print "Hello World"'
Hello World
5.10.0

If you want to enable new features, use the -E option instead:

$ perl -E 'say "Hello World"'
Hello World

You can also, of course, save the script in a file. Just remove the -e command line option and use the filename of the script: perl script.pl. For programs longer than a line, it's wise to turn on a couple of options:

use strict;
use warnings;

print "Hello World\n";

There's no real disadvantage other than making the code slightly longer. In exchange, the strict pragma prevents you from using code that is potentially unsafe and warnings notifies you of many common errors.

Notice the line-ending semicolon is optional for the last line, but is a good idea in case you later add to the end of your code.

For more options how to run Perl, see perlrun or type perldoc perlrun at a command prompt. For a more detailed introduction to Perl, see perlintro or type perldoc perlintro at a command prompt. For a quirky interactive tutorial, Try Perl.