Scalar values “typecast” to boolean valuesMany of Perl’s control structures look for a boolean value. Perldoesn’t have an explicit “boolean” type, so instead we use thefollowing “typecasting” rules for scalar values:If a scalar is a number, then 0 is treated as false, and any othervalue is treated as true.If a scalar is a string, then “0” and the empty string are treated asfalse, and any other value as true.If a scalar is not defined, it is treated as false.Unix Tools: Perl 2If elsif elseNote that both elsif and else are optional, but curly brackets arenever optional, even if the block contains one statement.if(COND){}[elsif{}]*[else{}]Unix Tools: Perl 2if-elsif-else examplesif example:if($answer == 12){print "Right -- one year has twelve months!\n";}Unix Tools: Perl 2if-elsif-else examplesif/else example:if($answer == 12){print "Right -- one year has twelve months!\n";}else{print "No, one year has twelve months!\n";}Unix Tools: Perl 2if-elsif-else examplesif-elsif-else example:if($answer < 12){print "Need more months!\n";}elsif($answer > 12){print "Too many months!\n";}else{print "Right -- one year has twelve months!\n";}Unix Tools: Perl 2if-elsif-else examplesif-elsif-elsif example:if($a eq "struct"){}elsif($a eq "const"){}elsif($a ne "virtual"){}Unix Tools: Perl 2defined() functionYou can test to see if a variable has a defined value with defined():if(!defined($a)){print "Use of undefined value is not wise!";}Unix Tools: Perl 2The while constructionwhile(<boolean>){<statement list>}As with if-elsif-else, the curly brackets are not optional.Unix Tools: Perl 2while exampleswhile(<STDIN>){print;}[You might note that we are using the implicit variable $_ in this codefragment.]Unix Tools: Perl 2until control structureuntil(<boolean>){<statement list>}The until construction is the opposite of the while constructionsince it executes the <statement list> until the <boolean>test becomes true.Unix Tools: Perl 2until example#!/usr/bin/perl -w# 2006 09 20 -- rdl script22.pluse strict;my $line;until(! ($line=<STDIN>)){print $line;}Unix Tools: Perl 2for control structurefor(<init>; <boolean test>; <increment>){<statement list>}Very similar to the C construction. The curly brackets again are notoptional.Unix Tools: Perl 2for examplefor($i = 0; $i<10; $i++){print "\$i*\$i = " . $i*$i . "\n";}Unix Tools: Perl 2Lists and ArraysA list in Perl is an ordered collection of scalars.An array in Perl is a variable that contains an ordered colletion ofscalars.Unix Tools: Perl 2List literalsCan represent a list of scalar valuesGeneral form:( <scalar1>, <scalar2>, ... )Unix Tools: Perl 2List literalsExamples(0, 1, 5) # a list of three scalars that are numbers(’abc’, ’def’) # a list of two scalars that are strings(1, ’abc’, 3) # can mix values($a, $b) # can have values determined at runtime() # empty listUnix Tools: Perl 2Using qw syntaxYou can also use the “quoted words” syntax to specify list literals:(’apples’, ’oranges’, ’bananas’)qw/ apples oranges bananas /qw! apples oranges bananas !qw( apples oranges bananas )qw< apples oranges bananas >Unix Tools: Perl 2List literals, cont’dYou can use the range operator “..” to create list elements.Examples:(0..5) #(0.1 .. 5.1) # same since truncated (not {\tt floor()}!)(5..0) # evals to empty list(1,0..5,’x’ x 10) # can use with other types...($m..$n) # can use runtime limitsUnix Tools: Perl 2Array variablesArrays are declared with the “@” character.my @a;my @a = (’a’, ’b’, ’c’);Notice that you don’t have to declare an array’s size.Unix Tools: Perl 2Arrays and scalarsArrays and scalars are in separate name spaces, so you can havetwo different variables $a and @a.Mnemonically, “$” does look like “S”, and “a” does resemble“@”.Unix Tools: Perl
View Full Document