Some Perl examples for new coders

For the OldCoder wiki index click here
Click here for the main OldCoder website


Overview

These are simply some Perl examples and notes for new coders. I may add other material from IRC and turn this into a full tutorial.

Feel free to edit and add code, questions, or comments.


Code Comments

This example illustrates comments.

# This Perl example illustrates comments. Comments are sim-
# ply  text lines that you add to your code in order to ex-
# plain things.

# Good Perl code,  or  good code  for any language,  should
# have plenty of comments.

# A Perl comment starts with # and continues to the end  of
# a source line.

#----------------------------------------------------------

# If  your code has different sections you can use a row of
# dashes as shown here to break things up and make the code
# more tidy.

#----------------------------------------------------------

# The comment style used  above is called "comment blocks".
# This means one or more lines of comments. There is also a
# style known as "inline comments". These are indented com-
# ments that  are used to  comment  on  one or two lines of
# code at a time. For example:

$x = 1; $y = 2;         # Set a couple of variables
$z = $x + $y;           # Add the variables
print "Sum is $z\n";    # Print the result

#----------------------------------------------------------

# In the C language, comments are done as follows:
#
#   int x = 1; /* Hello I am a C comment */
#   int y = 2; // I am a different type of C comment
#
# Bash and Python are like Perl:
#
#   # I am a Perl   comment
#   # I am a Bash   comment
#   # I am a Python comment
#
# PHP is interesting. It allows several types of comments:
#
#   /* I am a PHP comment */
#   // No, I am!
#   #  Me, too!
#
# HTML and XML have weird comments:
#
#   <!-- I am an HTML or XML comment -->
#
# Wikidot is weird too and similar to HTML and XML:
#
#   [!-- I am a Wikidot comment --]


Simple arrays #1

This example illustrates simple arrays.

# This  Perl example illustrates simple arrays. We'll  also
# start to use a new keyword  named "my"  here. Don't worry
# too much about "my" yet.

# Let's make a simple array:

my @food = qw (rice chicken beans corn);

# This  is a loop that prints the items in the array one at
# a time. For each item, $item gets set to the item and the
# print statement is done.

for my $item (@food) { print "I like $item\n"; }
print "\n";             # Add a blank line for readability


Simple arrays #2

# Hmm. What if we'd like the array to be sorted? O.K. Let's
# sort it:

my @food = qw (rice chicken beans corn);
   @food = sort @food;

# Print the list again. Now it is sorted:

for my $item (@food) { print "Let us worship $item\n"; }
print "\n";             # Add a blank line for readability

# Wait, what if we forgot a food? Let's add one.

push (@food, "bacon");

# Print the list again.  Now there is bacon.  Note that the
# list is no longer sorted because bacon was added.

for my $item (@food) { print "$item is good\n"; }
print "\n";             # Add a blank line for readability


Simple arrays #3

# qw, which we used above, is actually  a shortcut.  It can
# be used to make arrays quickly out of words.  The general
# procedure for making arrays is more  complicated but more
# flexible. Here is an example:

                        # Let's make some variables
my $animal      = "hamburger";
my $vegetable   = "potato";
my $mineral     = "potassium";

                        # Let's  put  some  variables, some
                        # numbers, and some strings all in-
                        # to an array together
my @bloodgate =
(
    $animal ,
    "Elony Bloodgate studied piano at school" ,
    42 ,
    $vegetable ,
    -16 ,
    $mineral
);

# For this approach to  creating an array,  there is no qw.
# Additionally, there must be a  comma  between  items. Text
# strings must be quoted.

                        # List the @bloodgate array
for my $humbug (@bloodgate) { print "$humbug\n"; }
print "\n";             # Add a blank line for readability


A general example

# Note: qw is special. Comments can't go inside a qw block.

                        # This is magic needed by games
srand (time() ^ ($$ + ($$ << 15)));

                        # List some reactions
my @reactions = qw
(
    accept  enjoy   dislike   hate       like
    love    reject  tolerate  criticize  praise 
);

my $item = "bacon";     # A tasty food
                        # Loop through some reactions
for my $reaction (@reactions)
{
    print "I $reaction $item\n";
}

print "\n";             # Add a blank line for readability
                        # Some foods (and we'll sort them)
my @food = sort qw (crackers fish bacon carrots banana);

                        # This is a nested loop
for my $food (@food)    # Process all  foods
{                       # Process next food 
                        # Process all  reactions
    for my $reaction (@reactions)
    {                   # Process next reaction

                        # This is a 50-50 cointoss
        my $cointoss = (rand (1) < 0.50) ? 1 : 0;

                        # Pick somebody randomly
        my $who  = $cointoss ? 'I' : 'You';

                        # Reaction to food
        my $text = "$who $reaction $food";

                        # Print reaction to food
        print "$text\n";
    }
}

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License