When writing a statement for perl to interpret in a source file, there is a great deal of flexibility available to the programmer.
The basic perl statement follows one of the following syntaxes (brackets indicate optional elements):
This is the main type of statement that you will write in perl. It is composed of an (optional) lvalue (with an optional, but highly recommended scope declaration, usually 'my') and assignment operator (we will speak more of these later), followed by an expression of some form, terminated with the semi-colon. Expressions in perl can take on a similar variety of forms, and spread across multiple lines, with interspersed comments.
print; #The simplest perl statement
$variable = $array[0];
my @array = ('one',
'two',
'three');
print "HELLO"
,"WORLD\n";
#Note above and below print different things, as newlines inside quotes are taken literally as part of the string
print "HELLO
WORLD\n";
my @array = (
"a",
# this is the a
"b", #this is the b
"c"
#this is the c
,"d" #this is the d
);
#Expressions and conditionals can also be spread across multiple lines
my $value =
(($a + $b)
/ $c
)
* $d;
Note, some EXPRESSIONS will contain BLOCKS (see below). Conditionals are like flow control statements (see below) which allow the programmer to specify test statements which will prevent the EXPRESSION from being evaluated under certain conditions.
This is the form of statement which allows a block of code to be conditionally executed depending on the evaluation of the flow_control statement and EXPRESSION. It may also use optional variables which are set up during each iteration of the flow_control statement (this is called an iterator pattern). We will talk more about flow_control statements later, this is just to get a feel for the variety present.
foreach my $value (@values) { print "GOT VALUE $value\n"; }
while (&we_have_data) {
#get the data
my $data =
&get_data();
print "${data}\n";
}
if (
defined($value)
&& (length($value) > 0)
&&!($value eq "NOT WANTED")
) {
print "$value\n";
}
{
#A local block where the strict pragma can be turned off for references (a bit advanced)
local no strict refs;
$value = undef;
}
map {
$_ =~ s/\s+/@/g;
} @values;
Page Information
|
Wiki Information |
Recent PBwiki Blog Posts |