Perl - Control Structures and Functions
Control structures
- Block statements
. A statement block is a sequence of statements enclose in curly braces { .... }
, as in { ++a$; $b/$a; } It can go wherever a statement goes and have the value of the last
statement and is true if not zero or empty, false otherwise.
If-else, if-elsif-else. It follows the general pattern: if (expression) { true statements } elsif
(expression) { truelsif statements) } else { falsestatements } Please note that statements end in
semi-colon but not the if control structure, it starts with curly braces and ends with curly
braces (required). See example 5
While/Until. These are loop control structures used respectively for a true expression
continue, or a false expression stop. Patterns: while (expression) { statements) } and until
(expression) { statements}. See example 5a
For/Foreach These are loop control expressions with a control variable or expression.
Patterns: for (initial-expression;test-expression;increment-expression) { statements } and
foreach $i (@somelist) { statements}. See example 6 You can omit the control scalar variable and Perl will
default to the $_ variable as a scratch variable (this is also true in many other circumstances). See example 6a
Functions/Subroutines
- Defining
. Subroutines are defined using the pattern: sub subname { statements } . They can be placed anywhere in the script, but usually are placed at the end. Be aware that the return value of a sub is the last expression evaluated, literally. See example 11
- Invoking
. You call or invoke a sub by putting an ampersand & before its name. Pattern: &subname . A sub can invoke, another sub, that in turn can invoke another, etc. There is no limit (other than efficiency) for the levels of invocation. The return value of a sub can be assigned to variables.
- Arguments
. A sub can be invoked with arguments. Pattern: &subname (var1, var2,varn) . The list of arguments is automatically assigned to @_ , and you can refer to the arguments in the sub as $_[i], remember that subscripts start from zero, so the first argument is $_[0], the second $ _[1], etc. See example 12
- Including
. To include a sub available in a library in your Perl directory (another Perl script), use the Pattern: require "scriptname.pl". Please note that subs you have in your own script you invoke like described above in item 2. You include a script that you have in your Perl lib sub-directory. In Windows (if you accepted the defaults) the Perl lib sub-directory should be in C:\Perl\lib; while in Linux it should be in /usr/lib/perl5/site_perl. Of course Perl comes with standard functions you can use.
Once you include, you can use all functions that the script supports. This is how we use specific subs created to support the Web. Of course, some times you can just modify an existing script and make it yours if it is freeware and the author allows you to do so.
Note: we will discuss libraries and modules in more details when we see perl object-oriented concepts.
This page is maintained by Al Bento
who can be reached at abento@ubalt.edu. This page was last updated on September 20, 2004. Although we will attempt to keep this information accurate, we can not guarantee the accuracy of the information provided.