Scripting Languages Perl Basics: Course: 67557 Hebrew University Lecturer: Elliot Jaffe - הפי טוילא
Scripting Languages Perl Basics: Course: 67557 Hebrew University Lecturer: Elliot Jaffe - הפי טוילא
Perl Basics
Course: 67557
Hebrew University
Lecturer: Elliot Jaffe – אליוט יפה
FMTEYEWTK
• Far More Than Everything You've Ever
Wanted to Know
• Perl
– Pathologically Eclectic Rubbish
Lister
– Practical Extraction and Report Language
• The Perl motto is TMTOWTDI
– ``There's more than one way to do it.''
TMTOWTDI
There's more than one way to do it
Larry Wall
Data Types
Values of any type may be stored in a variable
$myA = ’a’;
$myB = ”this is $myA string”;
$myC =
’this is another $myA string’;
Automatic Type Conversion
Conversion happens automatically
From To Conversion
“42” 42 String to Integer
42 “42” Integer to String
“3.14159” 3.14159 String to Float
3.14159 “3.14159” Float to String
“c” ‘c’ String to Char
‘c’ “c” Char to String
Perl Data Structures
• Scalar
• Arrays of Scalars
• Associative Arrays of Scalars – Hashes
• Sigil: $
• Holds a single scalar value of any type
$myVar[0] is 3
$myVar[2] is ’c’
• Access to array elements is by integer index
(zero based)
More on Arrays
• Creating and setting an element
$foo[3] = "dog";
• Assigning multiple element values
$foo[1,3] = ( "bear", "dear" );
• Adding new elements
@foo = ( @foo, "elk" ); # Append
@foo = ( "ace", @foo ); # Prepend
Sizes of Lists
• Two approaches yield two different results
@foo = ( "apple", "bat", "cat" );
• Get the number of elements contained in the list
$size = scalar( @foo ); # Yields 3
• Get the index for the last element contained in the
list
$size = $#foo; # Yields 2
Lists as LHS values
• You can use lists on the left-hand side of an
assignment "=" operator
($first, $last) = ("John", "Moreland");
• Perl uses "greedy" assignment for L-Values. Here,
$d is left untouched
($a,$b,@c,$d) = ("a","b","c","d","e");
• But, here, "e" is simply not assigned
($a,$b,$c,$d ) = ("a","b","c","d","e");
Range Operators
• Perl defines a special list range operator ".." to
simplify the specification of such a range
• The ".." operator is used as an infix operator
placed between any two scalar values
• Perl will interpolate the (quantized "in between")
values automatically
( 1..5 ) # Yields ( 1, 2, 3, 4, 5 )
( 1.3..6.1 ) # Yields ( 1.3, 2.3, 3.3, 4.3, 5.3 )
( 2..6, 10, 12 ) # Yields ( 2, 3, 4, 5, 6, 10, 12 )
( "a".."z" ) # Yields ( "a", "b", "c", ..., "z" ) Nice.
( "a1".."e9" ) # Yields ( "a1", "a2", ..., "e9" ) Wow!
Example
• Put “bat” between (“ape”, “cat”)
@foo = ( "ape", "cat" );
$foo[2] = "cat";
$foo[1] = "bat";
or
$a = shift( @foo );
unshift( @foo, $a, "bat" );
Builtin List functions
pop Remove last item $a = pop(@list);
$happy = 1;
print $happy ? “good" : “bad";
One line conditional
• Often used shortcut for if-then (then-if)
$happy = 1;
$good = 1 if $happy;
$bad = 1 if ! $happy;
For loop
for ($i = 0; $i < 10; $i++) {
print $i . “\n”;
}
for (;;) {
# infinite loop
}
Loops
while ( $foo = <FILE> ) {
# do stuff
}
do {
# stuff
} until ($end);
foreach
• Loop over a list
@list = (“dog”, “cat”, “fish”);
foreach $f (@list) {
print $f . “\n”;
}
Special loop modifiers
• next
– Restart loop with the next value
• last
– Exit loop
• redo
– Restart loop with the current value
Input/Output
• File handles are pointers to an I/O stream
• By convention they are in UPPERCASE
• No sigil
• Can be a pipe, socket, file
• Standard handles are
– STDIO, STDOUT, STDERR
print STDOUT “Hello World”;
open(FILEHANDLE, expression)
• For read:
open(INFILE, “<$fname“);
• For write:
open(OUTFILE, “>$fname);
• For appending
open(OUTFILE, “>>$fname);
• For random access:
open(FILE, “+>$fname”);
close(FILEHANDLE)
• Use to flush and close an open filehandle
close(INFILE);
Reading from FILEHANDLEs
• Scalar context reads one line
open(INFILE, “<$fname“);
while (<INFILE>) {
chop ($line = $_);
}
close(INFILE);
Reading from FILEHANDLEs
• List context reads entire file
open(INFILE, “<$fname“);
chop (@file = <INFILE>);
close(INFILE);
Numerical and Binary operators
+ Addition $i = 1 + 2; -- Decrement $i--;
% Modulus $i = 4 % 3; | OR $i = $i | 0xf
** Power $i = 2 ** 6; ^ XOR $i = $i ^ 2
/ Division $i /= 3;
% Modulus $i %= 3;
** Power $i **= 6;
String operators
. Concatenate $s = "Hello" . " " . "World";
.= Concatenate $s .= "!";
- Equals
x Replicate $s = ":)" x 32;
x Replicate $s x= 32;
-Equals
Comparison operators
Numeric String Two different operator
== eq types are confusing
!= ne
$i = 12;
< lt if ( $foo < 7 )
> gt # FALSE
if ( $foo lt 7 )
<= le # TRUE
>= ge
Compound Logical operators
|| OR $apples || $oranges
! NOT ! $fruit