This program prints out "hello".
#!/usr/bin/perl print "Hello.\n";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.plor by making it executable and running it:
$ chmod +x hello.pl $ ./hello.pl Hello.The symbol
\n tells Perl to print a new line.
This program prints a name.
#!/usr/bin/perl $name = "Dave"; print "Hello $name.\n";The name is contained in
$name.
This program does a calculation.
#!/usr/bin/perl $x = 1; $y = 2; $z = $x + $y; print "Z is $z.\n";It prints
$ ./calc.pl Z is 3.
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"; }It prints
1 x 5 = 5. 2 x 5 = 10. 3 x 5 = 15. 4 x 5 = 20. 5 x 5 = 25.
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"; }
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.
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"; } }It prints out
John was a Beatle. Paul was a Beatle. George was a Beatle. Ringo was a Beatle. Burton was not a Beatle.
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"; }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"; }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"; }
The facilities use strict; and my are not
compulsory. However, they are helpful to catch mistakes.
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";It prints out
There are 5 lemons left.
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"; }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.
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: $!";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.
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";It prints out
$ perl read.pl There are 12 lines in read.pl.
The while (<$f>) reads the file line-by-line.