perl data, scalar data types and control structures

42
Perl data, scalar data types and control structures

Post on 21-Dec-2015

227 views

Category:

Documents


0 download

TRANSCRIPT

Perl

data, scalar data types and control structures

General information about Perl

• PERL stands for Practical Extraction and Report Language OR Pathologically Eclectic Rubbish Lister—Perl's chief creator is Larry Wall (endorses both

names)

• These slides will cover perl version 5.6.1—ActivePerl 5.6.1 (for windows)

– http://www.activestate.com/Products/Language_Distributions/ choose ActivePerl Download link, then the MSI package.

• Perl is a script language—You must have the perl interrupter installed in

order to run perl scripts.

• Perl as a language—It has incorporated structures for many languages

such as c/c++, Lisp, FORTAN, and PL/I—And shell languages like bash, tcsh, and zsh

– This also means there are several ways to write the same statement. (see the hello world slide for a simple example)

—It has also added the functionality of many UNIX programs such as awk (and it’s syntax). Plus many other programs.

—Also it is case sensitive.—And uses type less data structures.

• Perl major strengths are in string manipulations and modules (to add extra functionality).

Getting help

• On UNIX only—man <perlfunc> where perfunc is the perl

function you need help with or information on

• On Windows and UNIX—perldoc <perlfunc>—perldoc –f <perlfunc>

• On the Web—http://www.perldoc.com

• Texts:—Learning Perl, Schwartz and Christiansen,

O'Reilly, 2000—Programming Perl, Wall, Christiansen and

Orwant, O'Reilly, 2000– Considered to be the Perl bible by many people.

The hello world script

#!/usr/bin/perlprint “hello World\n”; # normal print statement• ORprintf (“hello World\n”); #c like print statement• ORprint (“hello World\n”); # normal print

statement• ORprintf “hello World\n” ; #c like print statement

• Either of the 4 statements will work.

Comments

• Perl uses the # as a comment.• Start with the # and the comment ends at

the end of the line.• At the beginning of a line

—#this is a comment

• As in the last slide, it can be after statement(s)—print "Hi\n"; # comment

• There is no multi-line comment, like c/c++—Must comment each line.

Data

• Scalar data—numbers and strings of characters—4, 3.25, hello, etc.

• Numbers—Although we use integer and real numbers,

perl computes all numbers internally as double-precision floating-points values

– But you won’t notice.

—Also don’t look for integer operations as they don’t exist.

—floating point numbers, same as c language– 1.25, 7.25e45, -6.5e24, -12e-24, -1.2e-23

Data (2)

• Don't start a number with a Zero, unless it is the number zero.

• 0377 is 377 octal or 255 decimal• 0xff is hex ff or 255 decimal

—Both can use the negative sign as well.

• Note: string to number conversions, the leading zero(s) are ignored and assumed to be decimal.

strings

• The entire 256 character set can be used—NULL is not special in perl.

• Single-quoted —You can use any characters inside the single

quotes– except a single quote.– To use a single quote inside single quotes, you must

backslash the quote. example: '\' ' is a single quote.– To use a backslash followed by a single quote, you

need to backslash it. example: '\\\' ' gives you a \'

—‘hello’ is the five characters h, e, l, l, o—‘hello\n’ is hello\n—‘hello

there’ produces hello there

Strings(2)

• Double-quotes, (acts like C strings)—“hello world\n” produces hello world and a

newline—“new \007” produces new and bell sound—“Hello\tthere” produces Hello (tab) there—Some of the \ characters

– \n newline, \r return, \t tab, \b backspace, \a bell– \\ backslash, \” double quote, \cC <cntrl>-c– \007 Any octal ASCII value (007 is the bell)– \x7f Any hex ASCII value, (7f is delete)– \l lower case next letter, \L lower all letters until \E– \u upper case next letter, \U upper all letters until \E

• To test any of these, use the print command—so print “new \007” or print ‘hello\n’

Scalar Variables

• All scalar variables start with a $—So $i or $name

• equal sign works as normal—$i = 1 or $name= “Jim Ward”

• Perl does not require you to declare variables, especially since variables are type less—$i =1; #So $i has a number—$i = “Jim Ward”; # is a perfectly legal next statement.

• To declare a variable, you use the my operator—my $i; —We'll look a variable scope later on, when get to

subroutines. Right now, all variables are global

Operators

• numeric operators—same as c/c++ (without integer operators)—2 +3 (result 5), 2.1 -1.4 (result .7)—2 * 3 (result 6), 10 / 5 (result 2)—10.2 / 0.3 (result 34), 10 / 3 (result 3.33333…)—10 % 3 (10 mod 3 = 1)—exponentiation (FORTAN like)

– 2**3 (result 8) If the result is to big, you’ll get an error

• logical operators for numeric data—< <= == >= > != || (or) && (and)—produces a true/false value 5 > 2 = true

Quick word about True and False

• Perl uses the true/false standard of c/c++, with a perl twist.

• False—Zero, as well as the string "0"

– the string "0" is converted to Zero

—The empty string ""—Any undefined value (undef is reserved word

for it)– The last two happen more often than you might think.

• True—All negative and positive numbers—All non-empty strings (except "0")—Any defined variable that is not Zero, "", or "0".—IE: Anything not false

Operators for Strings

• Logical operators—uses FORTRAN notation—eq (==), ne (!=), lt (<), gt (>), le (<=), ge

(>=), || and &&

• Why different operators?—because of conversions using string operators 7

is greater then 30, since it uses the first number to decide, ie “7” > “3”, so “7” > “30”

• More operators—concatenation “hello”.”world” produces

“helloworld”—string repetition uses an x

– “jim” x 3 produces “jimjimjim”– “jim” x (4-2) produces “jimjim”– 3 x 4 produces “3333” or (2+1) x 4 produces “3333”

Converting between numbers and strings• perl does it for you, but not always the way you

think it should—print 4 * “2f”; produces 8—perl quietly and simply removes all non-numbers when

doing arithmetic operations.

• Converting to strings is very easy—$var = “x”. (4 * 2) produces “x8”—Even $var = “x”. 4*2 produces “x8”

– NOTE: $x = 4.23 is a floating value of 4.23– while $x = “4”. 23 and $x= “4”.”23”produces “423”– $x = 4. “23” results in an error (4.[a float] “23”, and no

operator).+ But $x= 4 . “23” works (note the space between 4 and the dot)

• Perl will does most of the work for you. To generate warnings see the Pragmas slide, then the first result, will warn you (but still do the operation).

binary operators

• $a += $b; # $a = $a + $b;• $a -= $b;• $a *= $b;• $a /= $b; # $a = a / b;• ++$a; $a++; ++($a + $b);• --$a; $a--;• $a .= “ this”; #$a = $a . “ this”;

chop and chomp

• chop removes the last character in the string—$x = “hello world”;—chop($x) #$x now has “hello worl”—$char = chop($x);

– $char has “l” and $x is “hello wor”

—You don’t have a variable in front of chop, it will throw away the result

—$x =""; chop($x); # $x still contains "";

• $x = “hello there\n”;• chop($x) #$x is “hello there”• chop($x) #$x is now “hello ther”

chop and chomp (2)

• chomp only removes the newline character

• $x = “hello there\n”;• chomp($x) #$x is “hello there”• chomp($x) # $x is still “hello there”

Variables and strings

• Examples of using variables inside strings• $fname = “jim”; $lname = “ward”;• $name = $fname . ’ ’ .$lname;

—OR $name = “$fname $lname”;—produce “jim ward”;

• $name = '$fname $lname';—produces '$fname $lname'

• use \u to upper next character—$name = “\u$fname \u$lname”;—produces “Jim Ward”

standard in and standard out

• <STDIN> (must be capped)—read in a until end line marker from the

standard in.—$a = <STDIN>; place input into $a—chomp($a); #remove EOL marker—print $a;

• <STDOUT> (again capped)—rarely used, since print by default goes to

standard out—print <STDOUT>, $a; same as print $a;

Creating a Perl script

• Create a text file with the perl script.• UNIX

—Need to tell the script where perl is located—#!/usr/bin/perl

– For linux machines.

• For Windows—The file extension needs to be .pl

– That is the extension that windows associates with perl, when ActivePerl is installed.

• Many people put the #! ... perl in the files, even on windows and use the .pl extension on UNIX—Mostly for consistently between the two

platforms.

Exercise 1

• Write a perl script that reads in a temperature in Fahrenheit and prints out the equivalent Celsius—Celsius = (Fahrenheit – 32) * 5 /9

—You will need <STDIN>, chomp, some arithmetic operations and a couple of print statements

Statements in perl

• The next set of slides will give you the syntax of the statements as well as many of variant syntax of the statements.—Some of the variants can be somewhat

confusing.

• First perl assumes a block of statements for all structures unless otherwise noted.

• Blocks of statements, uses the { } syntax—semicolons are required inside the block, but

not after the block itself.—note: perl assumes a semicolon after }

If and unless statements

if (T/F)block

if (T/F) blockelse block

if (T/F)block

elsif (T/F) block• can include more elsifs and 1

else

unless (T/F)block

unless (T/F)block

elseblock

unless (T/F)block

elsif (T/F) block• can include more elsifs and 1

else

• unless reverses the true and false side of the statement—If (T/F) true else false—unless (T/F) false else true

• using a single statement, instead of a blockstatement if (T/F); #No ; between statement and if

statement unless (T/F); #No ; between statement and unless

—There is no elsif or else part of these statement—A block in front will produce an error, because }

assumes a ; after, so it's a syntax error

trinary operator (like an if-then-else)

• COND ? THEN : ELSE—Can’t be statements, but can be nested

• $a = (T/F) ? $b : $c; if true $a=$b else $a=$c

• nested (leap year example)$leapyear =

$year % 4 ? 0 :$year % 100 ? 1 :

$year % 400 ? 0 : 1;—remember true is Non-zero, false = 0

• Makes for very unable code.

• Perl allows trinary operations on the left side as well

($a_or_b ? $a : $b) = $c;—So $c is assigned to either $a or $b, depending

on $a_or_b—Leads to really unreadable code.

Loops

• Many ways to implement loops, since it uses syntax from many languages

• Quick syntax—NOTE: the LABEL is optional.

• LABEL while (T/F) block• LABEL do block while T/F;• LABEL until (T/F) block• LABEL do block until T/F;• LABEL for (EXPR; T/F; EXPR) block• LABEL foreach variable (LIST) block

o NOTE: variable is also optional

• LABEL for variable (LIST) blocko NOTE: variable is also optional

while and for loops while( T/F) { statement; statement; … etc}

• Example:i= 1;while (i< 5) { i++;}ORi =1;do { i++;} while i<5;

for (expr;T/F;expr) { statements;}

• Example

for(i=1; i<5; i++) { i++;}• NOTE: the expr and T/F are

optional– Why? loop control, later.

for(;;) {++i;

}• legal loop, but infinite loop.

until loop

• Like the while loop, except it exits when the condition is true.

Example:i=1;until (i>=5) { i++;}

ORi=1;do { i++;} until i>=5;

Exercise 2

• Grade script• Write a perl script that reads numbers

between 0 and 100. It will stop reading when you enter a sentinel value of -1.

• It will print out the number and grade—90-100 A, 80-89 B, 70-79 C, 60-69 D, 0-59 F

• You need to write a loop and at least one if statement

for and foreach with lists

• with lists, for and foreach have the same syntax—common practice is to use foreach, to

minimize the confusion with the other for loop syntax

foreach $num (1,2,3,4,5,6,7,8,9) { statement;}foreach $name ("jim", "steve", "allyson",

"matt") {print "$name \n";

}

for and foreach with lists(2)

• Range values the .. tells perl to use a range of number—Only ascending order 1 to 10, but not 10 to 1.

foreach $num (1 .. 10) {statement;

}• OR evenforeach $num( 1,2,4 .. 8,10) {

statement;}• The true power of the foreach statement found

when using arrays.—So we'll get back to the foreach statement later.

for and foreach with lists(3)

• ranges with stringsforeach $letter ("a" .. "z") {

#whatever you might want to do

}foreach $letter ("aa" .. "zz") {

statements;}• $letter is aa, ab, ac, ad, … zx, zy, zz• "0" .. "z", produces 0 to 9• "a" .. "9", produces a through z

Loop control

• Perl provides the following statements for loop control—last LABEL

– exit the loop immediately

—next LABEL– skip rest of loop, and start the "next" cycle of the

loop.

—redo LABEL– restarts at the top of loop without evaluating

condition

—LABEL is optional, without LABEL command refers to inner most loop.

loop control examplesfor(i=0; i<10; i++) { if (i== 9) { ++i;

redo;• loop will execute with 10 and stop at

11 based on the for statement expr. the i++ will not happen, because of the redo.

}

next if i== 4;— start as if we are at the

end of the loop

last if i== 10;— exit loop

}

for(i=0;;i++) { if (i== 9) { ++i;

redo;• loop will execute with 10

}

next if i== 4;— start as if we are at the end of

the loop

last if i== 10;— exit loop

}

• same as on the left

labels and loop control

• With labels you can control nested loops• Example:OUTER: foreach $num (1..10) { INNER: for ($i=1; $i <10; $i++) {

next OUTER if ($i==4) && ($num ==2);next INNER if $i==2;print "$i times $num = ", $i*$num,"\n";

}}• So when i is 2, no output and when i is 4 and

num is 2 it will skip the rest of the INNER loop.

Bare blocks and looping.

• a LABEL can be included around a bare block and using loop control, we can create a loop

• Example:

LOOP: {if ($x > 5) {

$x--;next LOOP;

}$z = $x + $y;

}

case/switch statement

• Perl does not have a case/switch statement, but it can be created using bare blocks and loop control.

• example:SWITCH: {

if ($x == 1) { statements; last SWITCH;}if ($x >3) { statements; last SWITCH; }if ($x > 10) {statements; last SWITCH;}statements; # the default or else part

}• Really a series of if statements.

loops with the goto statement

• The horror: a goto statement• uses a LABEL• example:$i=0;TOP: goto END if $i >10;i++;goto TOP;END: print "using a goto statement\n";

Pragmas• You tell perl to be a little more helpful

—Such as warnings, forcing declarations of variables or even use integer functions.

• forcing variables declarations, normally found at the top of the program

use strict;• show warningsuse warning;• use integer functions instead of realuse integer;• To turn it off:no strict; no warning; no integer;• These are only the common ones, there are

dozens more.

QA&