DOC PREVIEW
FSU COP 4342 - Perl

This preview shows page 1-2-20-21 out of 21 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Fall 2006 Perl 02Scalar values “typecast” to boolean valuesMany of Perl’s control structures look for a booleanvalue. Perl doesn’t have an explicit “boolean” type, soinstead we use the following “typecasting” rules for scalarvalues:☞ If a scalar is a number, then 0 is treated as false, andany other value is treated as true.☞ If a scalar is a string, then “0” and the empty stringCOP 4342Fall 2006 Perl 02are treated as false, and any other value as true.☞ If a scalar is not defi ned, it is treated as false.COP 4342Fall 2006 Perl 02If elsif elseNote that both elsif and else are optional, but curlybrackets are never optional, even if the block contains onestatement.if(COND){}[elsif{}]*[else{}]COP 4342Fall 2006 Perl 02if-elsif-else examplesif example:if($answer == 12){print "Right -- one year has twelve months!\n";}COP 4342Fall 2006 Perl 02if-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";}COP 4342Fall 2006 Perl 02if-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";}COP 4342Fall 2006 Perl 02if-elsif-else examplesif-elsif-elsif example:if($a eq "struct"){}elsif($a eq "const"){}elsif($a ne "virtual"){}COP 4342Fall 2006 Perl 02defined() functionYou can test to see if a variable has a defined valuewith defined():if(!defined($a)){print "Use of undefined value is not wise!";}COP 4342Fall 2006 Perl 02The while constructionwhile(<boolean>){<statement list>}As with if-elsif-else, the curly brackets are notoptional.COP 4342Fall 2006 Perl 02while exampleswhile(<STDIN>){print;}[You might note that we are using the implicit variable$ in this code fragment.]COP 4342Fall 2006 Perl 02until control structureuntil(<boolean>){<statement list>}The until construction is the opposite of the whileconstruction since it executes the <statement list>until the <boolean> test becomes true.COP 4342Fall 2006 Perl 02until example#!/usr/bin/perl -w# 2006 09 20 -- rdl script22.pluse strict;my $line;until(! ($line=<STDIN>)){print $line;}COP 4342Fall 2006 Perl 02for control structurefor(<init>; <boolean test>; <increment>){<statement list>}Very similar to the C construction. The curly bracketsagain are not optional.COP 4342Fall 2006 Perl 02for examplefor($i = 0; $i<10; $i++){print "\$i * \$i = " . $i*$i . "\n";}COP 4342Fall 2006 Perl 02Lists and Arrays☞ A list in Perl is an ordered collection of scalars.☞ An array in Perl is a variable that contains an orderedcolletion of scalars.COP 4342Fall 2006 Perl 02List literals☞ Can represent a list of scalar values☞ General form:( <scalar1>, <scalar2>, ... )COP 4342Fall 2006 Perl 02List literals☞ Examples:(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 listCOP 4342Fall 2006 Perl 02Using qw syntaxYou can also use the “quoted words” syntax to specifylist literals:(’apples’, ’oranges’, ’bananas’)qw/ apples oranges bananas /qw! apples oranges bananas !qw( apples oranges bananas )qw< apples oranges bananas >COP 4342Fall 2006 Perl 02List literals, cont’d☞ You can use the range operator “..” to create listelements.☞ 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 limitsCOP 4342Fall 2006 Perl 02Array variables☞ Arrays are declared with the “@” character.my @a;my @a = (’a’, ’b’, ’c’);☞ Notice that you don’t have to dec lare an array’s size.COP 4342Fall 2006 Perl 02Arrays and scalars☞ Arrays and scalars are in separate name spaces, so youcan have two different variables $a and @a.☞ Mnemonically, “$” does look like “S”, and “a” doesresemble “@”.COP


View Full Document

FSU COP 4342 - Perl

Download Perl
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Perl and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Perl 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?