Author: marcus

Perl Subroutine

sub mysubroutine
{
print "Not a very interesting routinen";
print "This does the same thing every timen";
}

regardless of any parameters that we may want to pass to it. All of the following will work to call this subroutine. Notice that a subroutine is called with an & character in front of the name:

&mysubroutine; # Call the subroutine
&mysubroutine($_); # Call it with a parameter
&mysubroutine(1+2, $_); # Call it with two parameters

Parameters

In the above case the parameters are acceptable but ignored. When the subroutine is called any parameters are passed as a list in the special @_ list array variable. This variable has absolutely nothing to do with the $_ scalar variable. The following subroutine merely prints out the list that it was called with. It is followed by a couple of examples of its use.

The &printfirsttwo subroutine above also returns a value, in this case 1. This is because the last thing that subroutine did was a print statement and the result of a successful print statement is always 1.

Local variables

The @_ variable is local to the current subroutine, and so of course are $_[0], $_[1], $_[2], and so on. Other variables can be made local too, and this is useful if we want to start altering the input parameters. The following subroutine tests to see if one string is inside another, spaces not withstanding. An example follows.

Perl References

I’m happiest writing Perl code that does not use references because they always give me a mild headache. Here’s the short version of how they work. The backslash operator () computes a reference to something. The reference is a scalar that points to the original thing. The ‘$’ dereferences to access the original thing. Suppose there is a string…

$str = “hello”; ## original string

And there is a reference that points to that string…

$ref = $str; ## compute $ref that points to $str

The expression to access $str is $$ref. Essentially, the alphabetic part of the variable, ‘str’, is replaced with the dereference expression ‘$ref’…

print “$$refn”; ## prints “hello” — identical to “$strn”;

Here’s an example of the same principle with a reference to an array…

@a = (1, 2, 3); ## original array

$aRef = @a; ## reference to the array

print “a: @an”; ## prints “a: 1 2 3”

print “a: @$aRefn”; ## exactly the same

Curly braces { } can be added in code and in strings to help clarify the stack of @, $, …

print “a: @{$aRef}n”; ## use { } for clarity

Here’s how you put references to arrays in another array to make it look two dimensional…

@a = (1, 2, 3); @b = (4, 5, 6);

@root = (@a, @b);

print “a: @an”; ## a: (1 2 3)

print “a: @{$root[0]}n”; ## a: (1 2 3)

print “b: @{$root[1]}n”; ## b: (4 5 6)

scalar(@root) ## root len == 2

scalar(@{$root[0]}) ## a len: == 3

For arrays of arrays, the [ ] operations can stack together so the syntax is more C like…

The if…else Statement

This statement uses a relational expression to check the validity of a condition and execute a set of statements enclosed in braces. It returns a Boolean value, true or false, according to the validity of the condition. The syntax of the if…else statement is:

In this syntax, condition is a relational expression. If the result of this expression is true, then the block of statements following the if statement is executed. Otherwise, the block of statements following the else statement is executed.

In Perl, unlike other languages, all loops and conditional constructs require statements to be enclosed in braces, even for single statements.

In this example, the if clause checks whether $a is greater than $b. If the value of $a is greater than $b, then the result is: a is greater than b. Otherwise, the control transfers to the code following the else clause and the statement associated with the else clause is printed as a result.

The if…elsif…else Statement

This statement is used when there is more than one condition to be checked. The syntax of the if…elsif…else statement is:

In this syntax, if the condition associated with the if clause is false, the control transfers to elsif clause that checks for the next condition. The code associated with elsif clause is executed only if the condition is true or the code associated with the else clause is executed.