Lecture 6 - Perl Introduction
Lecture 6 - Perl Introduction
Perl Introduction
Perl
"Practical Extraction and Reporting Language"
written by Larry Wall and first released in 1987
Perl has become a very large system of
modules
name came first, then the acronym
designed to be a "glue" language to fill the gap
between compiled programs (output of "gcc",
etc.) and scripting languages
"Perl is a language for easily manipulating text,
files and processes": originally aimed at
systems administrators and developers
What is Perl?
Integer
25 750000 1_000_000_000
8#100 16#FFFF0000
Floating Point
1.25 50.0 6.02e23 -1.6E-8
String
‘hi there’ “hi there, $name” qq(tin can)
print “Text Utility, version $ver\n”;
Data Types
Boolean
0 0.0 “” "0" represent False
all other values represent True
Variable Types
Scalar
$num = 14;
$fullname = “John H. Smith”;
Variable Names are Case Sensitive
Underlines Allowed: $Program_Version = 1.0;
Scalars
usage of scalars:
print ("pi is equal to: $pi\n");
print "pi is still equal to: ", $pi, "\n";
$c = $a + $b
important! A scalar variable can be "used" before it is
first assigned a value
result depends on context
either a blank string ("") or a zero (0)
this is a source of very subtle bugs
if variable name is mispelled — what should be the
result?
do not let yourself get caught by this – use the "-w"
flag in the bang path:
#!/public/bin/perl -w
Variable Types
List (one-dimensional array)
@memory = (16, 32, 48, 64);
@people = (“Alice”, “Alex”, “Albert”);
First element numbered 0
Single elements are scalar: $names[0] = “Fred”;
Slices are ranges of elements
@guys = @people[1..2];
How big is my list?
print “Number of people: “, scalar @people, “ \n”;
Variable Types