Acessing function parameters in Perl

Watch out! This tutorial is over 6 years old. Please keep this in mind as some code snippets provided may no longer work or need modification to work on current systems.

Perl functions don’t have parameters, their arguments are passed in an array @_. You can simulate parameters by assigning to a list, but you can also just apply the usual array operations to @_.

Continue reading

Perl Postfix Control Constructs

Watch out! This tutorial is over 6 years old. Please keep this in mind as some code snippets provided may no longer work or need modification to work on current systems.

Control statements in Perl can also be written with the conditional following the statements (called “postfix”). This syntax functions (nearly) identically to the usual one you would expect.

Continue reading

Perl Hashes

Watch out! This tutorial is over 7 years old. Please keep this in mind as some code snippets provided may no longer work or need modification to work on current systems.

Variables whose names begin with % are hashes, which are essentially arrays subscripted by strings. As with arrays, %sue is a hash, and it is a different variable from $sue, though members of %sue are selected by $sue{$s}.

Continue reading

Perl Arrays

Watch out! This tutorial is over 7 years old. Please keep this in mind as some code snippets provided may no longer work or need modification to work on current systems.

Variables whose names begin with @ are arrays. If @sue is an array, it is different variable from $sue. However, members of @sue are selected by $sue[$i].

Continue reading

Hello World in Perl

Watch out! This tutorial is over 8 years old. Please keep this in mind as some code snippets provided may no longer work or need modification to work on current systems.

This is how you would display “Hello World” in Perl. It’s quite similar to Bash, but note the differences carefully!

Create a text file called hello_world.pl containing the following code:

#!/usr/bin/perl
#
# The traditional first program.

# Strict and warnings are recommended.
use strict;
use warnings;

# Print a message.
print "Hello, World!\n";

Navigate to a directory where your hello_world.pl is located and make the file executable:

$ chmod +x hello_world.pl

Now you are ready to execute your first perl script:

./hello_world.pl