Getting Started with Perl

nostalgicisolatedSoftware and s/w Development

Nov 4, 2013 (3 years and 11 months ago)

80 views

Getting Started with Perl

Jeffrey ROACH

28 November


What is Perl? What is it good for?


Perl is a scripting language


Perl is a prototyping language


Perl is designed for relatively short scripts


Perl programs are best written by a single
programmer


Perl is ideal for:


Test processing: System Administration, Back
-
end
web administration, Bioinformatics

Popularity of Perl

The decline of Perl


Replaced by PHP and Python


Syntax is very different from other languages


Programming constructs programmers expect are
non
-
supported


Subroutines are available, but weak


Object
-
oriented techniques are available, but weak
and slow


Perl is not suitable for large scale, multi
-
developer projects


Perl 6 has been coming next year for the last five
years

Nevertheless


Perl remains useful as a scripting language


Perl is installed in all Linux/Unix/Mac OS X
machines


Perl is easily installed on Windows (Active State)


Perl is free and a good place to start learning
good practice


Perl allows non
-
programmers to write small
programs that can do worthwhile things quickly

Learning Perl


Perl is a big, messy language


Two hours is not sufficient to even crack the
surface


What we will do:


Learn seven (7) basic concepts


Use this foundation to build some small, useful (at
least a little) tools


Decide whether you want to learn more on your
own:
Learning Perl The Hard Way

Getting Started on Kure or
Killdevil


Step 1: Get the course materials

cd

/
netscr
/<
your_onyen
>

cp

r /
netscr
/
roachjm
/Perl .

ls


Step 2: Choose an editor

nano

01_helloworld.pl

(OR)

vi 01_helloworld.pl


(OR)

e
macs

01_helloworld.pl

01_helloworld.pl

01_helloworld.pl

#!/
usr
/bin/
perl


use strict;

use warnings;


print "Hello, World!
\
n";


print 'Hello, World';


print "<
---

No New Line
\
n";


print 'Hello, World!
\
n';


print "
\
n";


Notes


Indicates which
perl

to use


Allows ./01_helloworld.pl


Use
chmod

u+x

<file.pl>


Strict and warnings pretty
much standard


Note distinction between “”
and ‘’


Note the new line
characer


\
n”

02_variables.pl

02_variables.pl

#!/
usr
/bin/
perl


use strict;

use warnings;


my $a = 1;

print "a: $a
\
n";


my $b = 2;

print "b: $b
\
n";


$b = $a + $b;

print "Added a to b.
\
n";

print "b: $b
\
n";



my $
first_name

= "Jeff";

my $
last_name

= "ROACH";

print "Full Name: $
first_name

$
last_name
\
n";




Notes


Usual start


Standard practice



Variables hold values


Values may be numbers or
strings


Perl is pretty promiscuous
about this

03_arrays.pl

03_arrays.pl



my
@a = (1,2,3,4,5);


print "@a
\
n";


print '$a[0] $a[1] $a[2] $a[3] $a[4]:';

print "
\
t$a[0] $a[1] $a[2] $a[3] $a[4]
\
n";


print '$a[
-
1] $a[
-
2] $a[
-
3] $a[
-
4] $a[
-
5]:';

print "
\
t$a[
-
1] $a[
-
2] $a[
-
3] $a[
-
4] $a[
-
5]
\
n";


my $
len_a

= scalar(@a);

for (my $
i
=0; $
i
<$
len_a
; $
i
++) {


print "$a[$
i
] ";

}

print "
\
n";



Notes


Arrays are variables that
hold more than one value


Individual values are
indexed by an integer


Array is @a


First element is $a[0]


Arrays can store numbers
and strings


Index must always be
integer

04_hashes.pl

04_hashes.pl



my
%a = ('one'=>1, 'two'=>2, 'three'=>3,
'four'=>4, 'five'=>5);


print "%a
\
n";


print '$a{
\
'one
\
'} $a{
\
'two
\
'} $a{
\
'three
\
'}
$a{
\
'four
\
'} $a{
\
'five
\
'}:';

print "
\
t$a
{'one'} $a{'two'} $a{'three'} $a{'four'}
$a{'five'}
\
n
\
n";


foreach

(keys %a) {


print "$_ => $a{$_} ";

}

print "
\
n
\
n";



Notes


Same as arrays, but with
arbitrary indices


Hash is %a


Element at ‘f’ is $a{‘f’}


Also called dictionaries,
associative arrays, maps

05_subs.pl

05_subs.pl

sub greeting {


my @
param

= @_;



print "Hello, $
param
[0], how are
you?
\
n
\
n";


return 0;

}


print "Round 1:
\
n";

foreach

(@names) {


greeting($_);

}

N
otes


Subroutines were the basis
for Structured Programming
circa 1970


1975


Natural way to break larger
programs into smaller
blocks


Improves readability, code
re
-
use, and code quality


Perl support is somewhat
underwhelming

06_scope.pl

06_scope.pl

my @names = ('Jeff', 'Jon',
'David', 'Sam');


sub scope1 {


print "In Scope1:
\
n";


print "
\
tNames
: @names
\
n";



my @
new_names

= ('
Ffej
',
'
Noj
', '
Divad
', '
Mas'
);


print "
\
tNew

Names:
@
new_names
\
n";

}


Notes


Scope is the value that
subroutines add


Scope restricts the value that
variables take to a particular
subroutine


Essentially a context


Prevents name conflicts and
allows larger programs to be
composed of smaller parts


Hierarchical


Concept expanded in object
-
oriented programming

07_files.pl

07_files.pl

sub
read_file

{


my @
params

= @_;



my $filename = $
params
[0];


open(FILE,'<',$filename) or die $!;



}



print "
\
nRead

File
\
n";

read_file
("File1.txt");


print "
\
nWrite

File
\
n";

copy_file
("File1.txt","File2.txt");


Notes

./07_files.pl File1.txt File2.txt

d
iff File1.txt File2.txt

c
p File1.txt File2CP.txt

d
iff file2.txt File2CP.txt



Read and write from files


Fundamental importance

08_echo.pl

08_echo.pl

#!/
usr
/bin/
perl


use strict;

use warnings;


my $
argc

= scalar(@ARGV);


print "@ARGV
\
n
\
n";


for (my $i=0; $i<$argc; $i++) {


print "$ARGV[$
i
] ";

}

print "
\
n
\
n";


foreach

(@ARGV) {


print "$_ ";

}

print "
\
n";


Notes

./08_echo.pl This is a test

e
cho This is a test



Uses @ARGV
builtin

array

09_stats.pl

09_stats.pl


my $
argc

= scalar(@ARGV); # You
can …


print "@ARGV
\
n";


my $sum = 0.0;

my $
sumsq

= 0.0;

for (my $i=0; $i<$argc; $i++) {


$sum = $sum + $ARGV[$
i
];


$sumsq = $sumsq + $ARGV[$i]*$ARGV[$i];

}


my $mean = $sum / $
argc
;

my $
stddev

=
sqrt
(($
sumsq

-

$sum*$sum/$
argc
) / ($
argc

-

1));


print "n: $
argc
\
n";

print "mean: $mean
\
n";

print "
stddev
: $
stddev
\
n";


Notes

./09_stats.pl 1 2 3 4 5




# denotes comments


Uses @ARGV
builtin

array



Single pass standard
deviation

10_cat.pl

10_cat.pl

sub
read_file

{


my @
params

= @_;



my $filename = $
params
[0];


open(FILE,'<',$filename) or die $!;



while (my $line = <FILE>) {


print $line;


}



close(FILE);


return 0;

}


Notes

./10_cat.pl File1.txt File2.txt

cat File1.txt File2.txt



Basic command line
args


Basic file I/O

11_wc.pl

11_wc.pl

sub
count_file

{


my @
params

= @_;



my $filename = $
params
[0];


open(FILE,'<',$filename) or die $!;



my $characters = 0;


my $words = 0;


my $lines = 0;



while (my $line = <FILE>) {



$characters = $characters + length($line);



my @
word_array

= split(' ',$line);



$words = $words + scalar(@
word_array
);



$lines = $lines + 1;


}



close(FILE);



return "$characters $words $lines";

}


Notes

./11_wc.pl File1.pl

w
c

File1.pl



Basic file I/O


Accumulator


String Split



Character, word, line order
reversed

12_cut.pl

12_cut.pl

sub
cut_file

{


my @
params

= @_;



my $filename = $
params
[0];


open(FILE,'<',$filename) or die $!;



while (my $line = <FILE>) {



chomp($line);



my @
line_array

= split(',',$line);



my $column1 = $
line_array
[0];


my $column4 = $
line_array
[3];



print "$column1,$column4
\
n";


}



close(FILE);



return 0;

}

Notes

./12_cut.pl File1.csv

cut

d ,
-
f 1,4 File1.csv



Split on comma


Print selected columns

13_grep.pl

13_grep.pl



while (my $line = <FILE>) {



chomp($line);



if ($line =~ m/System Sleep/) {




$times = $times + 1;




my @
line_array

= split(' ',$line);




my $date1 = $
line_array
[0];



my $date2 = $
line_array
[1];



my $time = $
line_array
[2];




print "System Sleep at: $date1 $date2 $time
\
n";


}


}


Notes

./13_grep.pl File1.log



Chomp procedure


If/then control structure


Regular expression



Log file analysis

14_head.pl

14_head.pl

sub
read_file

{


my @
params

= @_;



my $filename = $
params
[0];


open(FILE,'<',$filename) or die $!;



my $
lines_written

= 0;


while ((my $line = <FILE>) and ($
lines_written

< 10)) {


print $line;


$
lines_written

= $
lines_written

+ 1;



#Not allowed with strict


#if ($
lines_written

== 10) {


# break;


#}


}



close(FILE);


return 0;

}

Notes

./14_head.pl File1.log

h
ead File1.log



Use of
boolean

and in while


Failed use of break


Common use of comments

15_awk.pl

15_awk.pl

sub
hist_file

{


my @
params

= @_;



my $filename = $
params
[0];


open(FILE,'<',$filename) or die $!;



my %cities = ();



while (my $line = <FILE>) {



chomp($line);



my @
line_array

= split(',',$line);



if ($
line_array
[4]
eq

'"Active"') {




my $city = $
line_array
[0];



my $hours = $
line_array
[5];



Notes


./15_awk.pl File2.csv



AWK is actually is own
programming


String
eq

not =

15_awk.pl

15_awk.pl



my $
city_total

= $cities{$city};



if ($
city_total
) {



$
cities{$city} = $
city_total

+
$hours;



} else {



$cities{$city} = $hours;



}


}


}



close(FILE);



return %cities;

}


Notes


If/Then/Else checking for
key


Returns cities hash

Conclusions


Perl has tons of possibilities


Expressive: You can write things a million
different ways


Useful: You can make useful little tools relatively
easily


Organic development and prototyping


For further self
-
study:


Learning Perl The Hard Way


There is also Python, Ruby, PHP, and
Lua