Perl tutorial for beginners

Print hello

This program prints out "hello".

#!/usr/bin/perl
print "Hello.\n";

(download)

The first line containing #!/usr/bin/perl instructs Unix that the rest of the file is a program to send to /usr/bin/perl.

The program may be run like this:

$ /usr/bin/perl hello.pl

or by making it executable and running it:

$ chmod +x hello.pl
$ ./hello.pl
Hello.

The symbol \n tells Perl to print a new line.

Print a name

This program prints a name.

#!/usr/bin/perl
$name = "Dave";
print "Hello $name.\n";

(download)

The name is contained in $name. It prints

Hello Dave.

Do a calculation

This program does a calculation.

#!/usr/bin/perl
$x = 1;
$y = 2;
$z = $x + $y;
print "Z is $z.\n";

(download)

It prints

$ ./calc.pl
Z is 3.

Do several calculations

This program does calculations on a set of numbers.

#!/usr/bin/perl
@numbers = (1, 2, 3, 4, 5);
for (@numbers) {
    print "$_ x 5 = ", $_ * 5, ".\n";
}

(download)

It prints

1 x 5 = 5.
2 x 5 = 10.
3 x 5 = 15.
4 x 5 = 20.
5 x 5 = 25.

Split a string

This program splits a string into a list of strings and prints them.

#!/usr/bin/perl
$beatles = 'John Paul George Ringo';
@names = split / /, $beatles;
for (@names) {
    print "$_ was a Beatle.\n";
}

(download)

It prints

John was a Beatle.
Paul was a Beatle.
George was a Beatle.
Ringo was a Beatle.
split / /, $beatles tells Perl to split $beatles at the space characters.

Compare a string

This program compares strings.

#!/usr/bin/perl
@beatles = ("John", "Paul", "George", "Ringo", "Burton");
for (@beatles) {
    if ($_ eq "Burton") {
        print "$_ was not a Beatle.\n";
    }
    else {
        print "$_ was a Beatle.\n";
    }
}

(download)

It prints out

John was a Beatle.
Paul was a Beatle.
George was a Beatle.
Ringo was a Beatle.
Burton was not a Beatle.

Making a mistake

If there is a mistake like @names instead of @beatles in the following, Perl does not complain:

#!/usr/bin/perl
@beatles = ("John", "Paul", "George", "Ringo");
for (@names) {
    print "$_ was a Beatle.\n";
}

(download)

It just prints out nothing.

Perl can find these mistakes if the line use strict; appears at the top of the program:

#!/usr/bin/perl
use strict;
@beatles = ("John", "Paul", "George", "Ringo");
for (@names) {
     print "$_ was a Beatle.\n";
}

(download)

This prints out

Global symbol "@beatles" requires explicit package name at compare_mistake.pl line 3.
Global symbol "@names" requires explicit package name at compare_mistake.pl line 4.
Execution of compare_mistake.pl aborted due to compilation errors.

To make the program operate correctly, use my in front of @beatles:

#!/usr/bin/perl
use strict;
my @beatles = ("John", "Paul", "George", "Ringo");
for (@beatles) {
     print "$_ was a Beatle.\n";
}

(download)

The facilities use strict; and my are not compulsory. However, they are helpful to catch mistakes.

Make a dictionary

This program tells us how much fruit we have left:

#!/usr/bin/perl
use strict;
my %fruit = (
    "banana" => 3,
    "lemon" => 5,
    "pineapple" => 9,
);
my $type = "lemon";
print "There are $fruit{$type} ${type}s left.\n";

(download)

It prints out

There are 5 lemons left.

Get input from the user

This program gets input from the user.

#!/usr/bin/perl
use strict;
my %fruit = (
    "banana" => 3,
    "lemon" => 5,
    "pineapple" => 9,
);
print "What kind of fruit: ";
my $type = <STDIN>;
chomp $type;
if (defined $fruit{$type}) {
    print "There are $fruit{$type} ${type}s left.\n";
}
else {
    print "We don't have any ${type}s.\n";
}

(download)

The user must type something:

$ perl input.pl
What kind of fruit: lemon
There are 5 lemons left.
$ perl input.pl
What kind of fruit: monkey
We don't have any monkeys.

The user typed "lemon" and then "monkey".

The <STDIN> gets a line from the user. The chomp $type; removes the final carriage return character from $type.

Open a file

This program opens a file called "output.txt" and prints something into the file.

#!/usr/bin/perl
use strict;
open my $f, ">", "output.txt"
    or die "Can't open the file: $!";
print $f "Hello file.\n";
close $f
    or die "Can't close the file: $!";

(download)

Usually this does not print anything on the terminal.

The open opens the file. The ">" says that the file is for writing to. The close closes the file.

The or die line after open and close catches errors which might happen. For example, if the above program is run in a directory where the user is not allowed to write a new file, Perl prints an error message:

$ cd /
$ perl ~/lemoda/perl/perl-tutorial-for-beginners/open.pl
Can't open the file: Permission denied at /home/ben/lemoda/perl/perl-tutorial-for-beginners/open.pl line 3.

The operating system refused to allow a new file called "/output.txt" to be opened in the top directory.

Read a file

This reads a file:

#!/usr/bin/perl
use strict;
my $file_name = "read.pl";
open my $f, "<", $file_name
    or die "Can't open $file_name: $!";
my $lines = 0;
while (<$f>) {
    $lines += 1;
}
close $f
    or die "Can't close $file_name: $!";
print "There are $lines lines in $file_name.\n";

(download)

It prints out

$ perl read.pl
There are 12 lines in read.pl.

The while (<$f>) reads the file line-by-line.


Copyright © Ben Bullock 2009-2023. All rights reserved. For comments, questions, and corrections, please email Ben Bullock (benkasminbullock@gmail.com) or use the discussion group at Google Groups. / Privacy / Disclaimer