More...

Perl Controls

First line of perl code

The first line of a perl program should contain the path location where perl is to be found on your system.

	#!/usr/bin/perl

You can add the -w switch to provide extra warning messages.

	#!/usr/bin/perl -w

top of page


=~ String searching

The '=~" syntax is used to find/match specific contents in a variable.

$x = "One two three"
if ($x =~ /two/)  (is true)

Use the '^' to signify 'starting with'

if ($x =~ /^two/)   (is false, starts with one)

Use a trailing 'i' command to ignore case

if ($x =~ /one/i)  (Is true because case is ignored)

Use '\b' to check for word boundaries

if ($x =~ /thre/)  (Is true because 'thre' of 'three' is found)
if ($x =~ /thre\b/) (Is false because there is no word 'thre')

Use '\W' to find the first non-word character.
Use '.*' to represent current find results to end of value
Use a starting 's' to represent substitute and a trailing 'newvalue/'
By combining these commands you can extract the first word in a variable.

$saveoriginal = $x;
$x =~ s/\W.*//;

value of $x is now 'One'.

top of page


Comparison Operators
Comparison Numeric String
Equal == eq
Not Equal != ne
Less than < lt
Greater than > gt
Less or equal to <= le
Greater or equal to >= ge

top of page


String Concatation

String values are concatenated using the period as an operator.

$string1 . "more text";
$string1 . " " . $string2;

top of page


Auto increment or decrement

The ++ or -- symbols are used to increment or decrement a variable by one. Depending on whether the ++ or -- comes before or after the variable determines when it is incremented.

$d = 17;
$e = ++$d;   # $e and $d both equal 18
$d = 17;
$e = $d++;  # $e = 17 and $d = 18
$x = 12;
--$x;  # $x = 11
$y = $x--  # $y = 11, $x = 10

top of page


if, else, elsif

Simplist form - if only

if (some expression) {
    true statement;
}

Using else

if (some expression) {
    true statement;
} else {
    false statement;
}

Using elsif

if (some expression) {
    true statement;
} elsif (another expression) {
	another truestatement;
} else {
    false statement;
}

top of page


while, until

The while loop is used to keep performing a loop while an expression is true.

while (true expression) {
    statement 1;
    statement 2;
}

The until loop is used to keep perfoming a loop while an expression is false.

until (false expression) {
    statement 1;
    statement 2;
}

top of page


last, next, redo

The last statement is used to break out of a block loop. In the following example, if the 'somecondition' expression is true, the 'dothesestatements' is executed the first time along with last. The loop is immediately exited and the 'morestuff' doesn't get executed.

while (something) {
    statements;
    if (somecondition) {
        dothesestatments;
        last;
    }
    morestuff;
}
# last jumps to here

The next statement is used to break from a inner, jump to the end of the loop, and continue the loop.

while (something) {
    statements;
    if (somecondition) {
        dothesestatements;
        next;
    }
    morestuff;
    # next jumps to here & continues loop
}

The redo statement is used to jump to the top of the loop without re-evulating the control expression.

while (something) {
    # redo jumps to here & continues loop
    statements;
    if (somecondition) {
        dothesestatements;
        redo;
    }
    morestuff;
}

top of page


Labeled blocks

Labeled blocks work similiar to 'goto' in other languages. It is recommended that the label name is in all upper case and only use letters or numbers. Also use a name that isn't or won't ever be used as reserved word, i.e. 'print' would not be a legal name to use for label. The name is always followed by a colon.

SOMELABEL: while (condition) {
    statements;
    if (somethingelse) {
        otherstatements;
        last SOMELABEL;
    }
}

top of page


Expression modifers

For if, unless, while and until there is a shortcut syntax.

(do_this) if (this_is_true)

(do_this) unless (this_is_true)

(do_this) while (this_is_true)

(do_this) until (this_is_true)

top of page


Logical AND && and OR ||

The && is used to test that two expression are true. The following only gets executed if both expressions are true.

if ( tihs_is_true && also_true ) {
}

The || is used to that either express is true. The following gets executed if one or the other expression is true.

if ( this_is_true || or_true ) {
}

top of page


for

The for statement performs a loop as long as a a tested expression is true, increasing the value for each loop.

for ( initial_expression; test_expression; increment_expression ) {
    statement1;
    statement2;
}

The following example prints the numbers 1 to 10.

for ($i = 1; $i <=10; $i++) {
    print "$i ";
}

top of page


foreach

This statement takes a list of values and assigns them one at a time to a scalar variable. Each time executing a block of code.

foreach $i (@some_list) {
    statement1;
    statement2;
}

The following example takes in element and mutliples it by 3 and replaces the original values in the array.

@a = (3, 5, 7,9);
foreach $one (@a) {
    $one  *= 3;
}

@a now equals = 9, 15, 21, 27

top of page


Pattern matching

To search for patterns in a string there are many options that can be used. The values to match should be enclosed in '/ /'s. The following table defines the options and provides examples.

Construct What it matches Example Results
* Any number of characters /ab*c/ Value must contain a, any number of b's and c
. (dot) Single character, except newline (\n) /a./ Value must contain a plus any single character
^ Beginning of value /^a*/ Value must start with a and number of characters
$ End of line /stop$/ Value must end with stop
[values] Anyone of the characters /[aeiou]/ Value must contain a lowercase a,e,i or u
trailing letter i Ignore case /roger/i Matches roger, ROGER or any combination of upper lower case
[from-to_range] or [\d] A range of values for a single character /[0-9]/ or /[\d]/ Value must be in the range of 0-9
[^from-to_range] or [\D] Not match /[^0-9]/ or /[\D]/ Value must not be in the 0-9 range
[a-zA-Z0-9] or [\w] Any word value /[\w]/ Matchs a word
[^a-z]A-Z0-9] or [\W] Not match any word value /[\W]/ Must not equal a word
[ \r\t\n\f] or [\s] Match any whitespace /[\s]/ Must match whitespace
+ Match one or more of the proceeding character /fo+bar/ Must contain f, one ore more o's and bar
? Match zero or more of proceeding character /fo+ba?r/ Must contain f, one or more o's, b, optional a and r
(value) and \number Remembers the value found with in the '()'s and can be referenced by the \number. The \number value refers to the number of ()'s used from left to right. /fred(.)barney\1/ Matches fredXbarneyX, but not fredXbarneyYY
| (vertical bar This works as an OR operator /(song|blue)bird/ Must match songbird or bluebird
m + new delimiter value Change the standard delimiter of forward slash to another character. Useful if the matching value contains forward slashses, such as full path names. m#^/usr/etc#

same as

^/\/usr\/etc/

Matches /usr/etc at the beginning of the line.

There are three read-only variables that automatically get set as a result of a pattern maching command.

$` = all content before matched value
$& = the matched value
$' = all content after matched value

$_ = "this is a sample string of characters";
/sample/;

$` = this is a
$& = sample
$' = string of characters

top of page


Substitution

The syntax s/match_value_to_replace/replace_value/ replaces the value searched for with the replace_value. By adding the letter g to the end, will find all possible matches. Otherwise, only the first match found is changed.

$_ = "here where there theresa";
s/ere/at/g; # $_ = hat, what, that, thatsa
$_  = "hello, world";
$new = "goodbye";
s/hello/$new/;  # $_ = goodbye, world
$_ = "this is a test";
s/(\w+)/<$1>/g # $_ = <this> <is> <a> <test>

The above works, because $1 is set to the with in the first parenthesized pattern match, which is this case is each word because the 'g' flag is used.

top of page


split and join

The split() function breaks apart an expression based on a delimiter value. This can be used to parse a scarlar variable into an array so each field can be treated as a single element.

$value = "field1:field2:lastfield";
@fields = split(/:/,$value);

@fields = "field1", "field2", "lastfield"

The join() function is used to convert the array back to a single variable. The first value in the expression represents the delimiter value or expression to use.

$bigfield = join("AND", @fields);

$bigfield = "field1ANDfield2ANDlastfield"

top of page


chomp and chop

chomp removes the trailing 'newline' (\n) value from the end of a scalar variable.

$x = "value\n";
chomp($x);   # $x = "value"

chop removes the last character from a variable.

$x = "abcdefg";
chop($x);    # $x= abcdefg
$a = chop($x);   # $a = g,  $x = abcdef

top of page


qw

Used to set values in array or hash variable without using quotes.

@x = qw(value1 value2 value3);
%x = qw(
    key1 value1
    key2 value2
    key3 value3
);

top of page


Define a Function - sub

Functions work like subroutines. They can appear anywhere with the code and are only executed when called. The normal program flow will skip over subroutines. The standard method is to place them at the end of the code. All variables used within the subroutine are global unless otherwise specified.  A subroutine is identified by the word 'sub', followed by the name of the subroutine. The folowing code associated with the sub must be surrounded by '{ }'s.

sub subname {
    statement1;
    statement2;
}

top of page


Using a Function

You call a subroutine by using the subroutine name followed by '( )'s. Examples...

subname();
$a = 3 + subname();
for ( $x = subname1(); $x < subname2(); $x += subname3() ) {
	statements;
}

top of page


Return Values of a Function

A subroutine always returns an expression. This is controlled by a 'return' statement or the last expression evaluated. Note: The last expression evaluated literally means the last expression evaluated, and not just the last expression within the subroutine.

sub sum_a_b {
    return $a + $b
}
$a = 3;
$b = 4;
$c = sum_a_b(); # $c = 7
$d = 3 * sum_a_b(); # $d = 21

You can return a list of values by enclosing the return value in '( )'s.

sub list_a_b {
    return($a, $b);
}
$a = 5;
$b = 6;
@c = list_a_b(); # @c = 5, 6

The following demostrates the last expression logic. The following returns $a if $a >0, otherwise it returns $b.

sub get_a_b {
    if ($a >0 ) {
        print "chosing a ($a) \n";
	return $a;
    } else {
        print "chosing b ($b) \n";
        return $b;
    }
}

top of page


Function Arguments

To pass arguments or values to a sub routine, you place the variables with the '( )'s following the name of the subroutine. These variables are automatically placed in a variable '@_' that is private to the subroutine.

Within the subroutine, you can refer to the different elements of the array '@_' by using '$_[x]'.  Where x = the element number. Thus if you called a subroutine as...

do_sub(value1, value2);
do_sub() {
    print "@_"; # prints value1, value2
    print "$_[0]"; # prints value1
    print "$_[1]"; # prints value2
}

In re-writing the above sum_a_b routine to pass any variable names it would look like...

sub sum_a_b {
    return $_[0] + $_[1]
}
print sum_a_b(3,4); # prints 7
$c = sum_a_b(5, 6); # prints 11

To handle unlimited or a variable list of passed arguments, you can use a foreach loop and the '@_'  variable.

sub add {
    $sum = 0;
    foreach $_ (@_) {
        $sum += $_;
    }
return $sum;
$a = add(4,5,6); # $a = 15
print add(1, 2, 3, 4, 5); # prints 15
print add(1..5); # also prints 15 because 1..5 is expanded

top of page


Private Variables in Functions

One private variable is the '@_' and access to it's elements by $_[x]. You can create your private variables by use of the my operator. my variables are strictly used within the subroutine where there are defined. my variables can only be scalar, array or hash. The following example uses variable names that it more readable. In addition it passes an extra value that is used for a limits check.

sub bigger_than {
    my ($limit, @values);
    ($limit, @values) = @_;
    my(@result);
    foreach $_ (@values) {
        if ($_ > $limit) {
            push(@result, $_);
        }
    }
    return @result;
}
@new = bigger_than(100, @list); # @new contains all values in @list >100
@this = bigger_than(5,1,5,15,30); # @this = 15,30

top of page


SemiPrivate Variables in Functions

The local operator is used to define variables of any type that are private to the subroutine there are defined in and any subroutine called within the defininf subroutine.

$value = "original";
tellme();
spoof();
tellme();
sub spoof {
    local ($value) = "temporary";
    tellme();
}
sub tellme {
    print "Current value is $value\n";
}

This code cause the sub tellme to be executed 3 times, printing the results as follows...

first time, prints "original"
second time, prints "temporary"
third time, prints "original"

top of page

Send Email © Advanced Horizons, Inc. All Rights Reserved