Perl, an open source interpreted language.
Developed by Larry Wall (linguist) in 1987 named it after the Parable of the Pearl. Perl is known as Practical Extraction and Reporting Language or Pathologically Eclectic Rubbish Lister.
Widely adopted for strengths in string processing, parsing or finding strings in text files. Text manipulation for:
It is practical (as in easy to use, efficient) rather than beautiful (elegant). It requires less programming time but requires more computer resources.
It borrows features from C, shell scripting, awk, sed and Lisp.
Automatic memory management, dynamic typing (automatic data typing).
file.pl -> compiler -> Parse tree (syntax tree) -> interpretation
compile time (develop the syntax tree) -> run time (walk the tree)
The syntax tree is an AST: Labeled directed tree, where nodes are operators and leaf nodes are operands.
There are a ton of excellent Perl resources on the web. A good one is Essential Perl from Nick Parlante, who's at
Stanford.
Another good one is a Perl tutorial from the University of Leeds.
Also, the Beginner's Introduction to Perl from O'Reilly
is good. You can easily go through these tutorials by yourself.
In 15-123, we will cover:
Variables: scalar, array, hash all start with a sigil (a symbol attached to a
variable name).
Data types:
Variables have a leading sigil:
$foo is a scalar
@foo is an array
%foo is a hash
FOO is a file handle
&foo is a subroutine
Let's move to code:
our first perl program hello.pl
can you execute hello.pl?
chmod u+x filename
let's look at hello.pl
#!/usr/local/bin/perl -w <- for warnings
print "hello world!\n"; <- required to separate statements except for last
line.
! is a bang
#comment
#! shebang (tells OS where to find perl)
how about more_hello.pl
print "Hello ..." <- force evaluation
print 'Hello World!\n'
Scalar Variables:
$hello = 'hello world!';
print `$hello\n`;
print "$hello\n";
$hello = *2;
print "$hello\n";
Operators:
okay, let's go through examples in test.pl
Arrays:
example:
@arr = (1,2,"3");
$arr[0] = 1;
Note - arrays are not bound checked, if reading element outside array undef is
returned.
$arr[99] = "hi";
array now is 100 elements.
How about some array examples in arrays.pl
How about shift, push, pop and splice arrays.
What about @ARGV