Quick Navigation Bar data structures :: program control :: input/output and file i/o [ toc | forums ] |
Note: If the document URL does not begin with https://randu.org/tutorials/perl/ then you are viewing a copy. Please direct your browser to the correct location for the most recent version. |
if
and else
statements are used as
conditionals as in any other language. For example:
if ($cow < $sheep) { print "baaah....\n"; } else { print "moo....\n"; }
unless
statement is specific to Perl, in
which you want to always just evaluate the false part instead of
doing something when not true. For example:
unless ($age < 21) { print "Why yes... you can drink bub.\n"; }Thus we proceed into the block if the above expression is false.
elsif
is also specific to Perl, which simply combines
an else
and an if
together in one:
if ($a < $b) { $z++; } elsif ($b < $c) { $y++; } elsif ($c < $d) { $x++; } else { $yikes++; }
while
loop:
while ($food < $stomach_ache) { $plop++; $fizz--; $oh_what_a_relief_it_is *= 5; }
unless
statement above, it is easier to
say "until something is true" (Schwartz 61). Thus, Perl provides
the until
statement:
until ($x > 6) { print "$x is the number!\n"; $x++; }
do, while
loops, but
Perl also adds do, until
loops:
# do while example do { $junk++; } while ($junk < 39); # do until example do { $x++; $aok = @array_hooray[$x]; } until ($aok eq 'loop_de_loop');
for
statement? These loops works just like C:
for ($i = 0; $i < 2; $i++) { print "$i\n"; }
foreach
loop control statement. foreach
allows
you to assign an element out of a list and assigns it to a scalar.
@array_whoop = (0, 1, 2, 3); foreach $x (@array_whoop) { $x += 2; } # @array_whoop now is (2, 3, 4, 5); foreach (@array_whoop) { print; }What would the last foreach loop do? It would print out each element in the list without even having to specify a scalar in the loop control statement. This is because Perl uses a special
$_
scalar as "scratch" space. So the foreach and print is implied.last
,
next
, redo
. last
works like
a break statement in C, which allows you to break out of a loop.
next
is similar to the continue statement in C, but is
a little different in that it allows you to skip to the end of the block
but without terminating the block. redo
allows you to
jump to the beginning of the block without re-evaluating the control
conditions of the loop.if
to modify expressions:
($a = $b) if ($a < $c); # equivalent to: if ($a < $c) { $a = $b; }You can also modify expressions using
while, until,
unless
.sub
.return
. For example:
sub useless { return ($x, $y); # $x, $y are global variables } @z = useless(); # @z contains $x, $y
@_
to store arguments. This array is private
to the function. For example:
sub add_two_nums { return $_[0] + $_[1]; } $x = add_two_nums(1, 2);
my
operator: my ($a)
used inside a subroutine would result in a temporary, local variable
called $a. If there is already a global variable $a, it will be stored
somewhere else, so you can name local variables the same as global
ones. You can do multiple my's in one call and even initialize:
sub something { my ($x, $y, $z) = 0; }
local
operator. Semiprivate variables can be used across
other functions. For example:
sub sub1 { local ($x) = 1; sub2(); } sub sub2 { print "$x\n"; }Would print 1 to the screen.
local $_;
at the top of an subroutine that uses
$_.use strict;Tells Perl to turn on the strict pragma. With this turned on, you must declare all variables before using them. Thus you must use the
my
operator with all variables before
using them:
my $x; my @a = (1, 2, 3); $z = 3; # ERROR! z isn't declared
use strict;
pragma to enforce good programming
practice!
Notice: Please do not replicate or copy these pages and
host them elsewhere. This is to ensure that the latest version can always
be found here.
Disclaimer: The document author has published these pages
with the hope that it may be useful to others. However, the document
author does not guarantee that all information contained on these
webpages are correct or accurate. There is no warranty, expressed or
implied, of merchantability or fitness for any purpose. The author does
not assume any liability or responsibility for the use of the information
contained on these webpages.
If you see an error, please send an email to the address below indicating
the error. Your feedback is greatly appreciated and will help to
continually improve these pages.