Getting Started with Perl Jeffrey ROACH 28 November
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, multideveloper 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 vi 01_helloworld. pl emacs 01_helloworld. pl (OR)
01_helloworld. pl Notes #!/usr/bin/perl • Indicates which perl to use strict; use warnings; print "Hello, World!n"; print 'Hello, World'; print "<--- No New Linen"; print 'Hello, World!n'; print "n"; – 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 Notes #!/usr/bin/perl • Usual start • Standard practice use strict; use warnings; my $a = 1; print "a: $an"; my $b = 2; print "b: $bn"; $b = $a + $b; print "Added a to b. n"; print "b: $bn"; … my $first_name = "Jeff"; my $last_name = "ROACH"; print "Full Name: $first_name $last_namen"; … • Variables hold values • Values may be numbers or strings • Perl is pretty promiscuous about this
03_arrays. pl … my @a = (1, 2, 3, 4, 5); print "@an"; 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 Notes … my %a = ('one'=>1, 'two'=>2, 'three'=>3, 'four'=>4, 'five'=>5); • Same as arrays, but with arbitrary indices • Hash is %a • Element at ‘f’ is $a{‘f’} • Also called dictionaries, associative arrays, maps print "%an"; print '$a{'one'} $a{'two'} $a{'three'} $a{'four'} $a{'five'}: '; print "t$a{'one'} $a{'two'} $a{'three'} $a{'four'} $a{'five'}nn"; foreach (keys %a) { print "$_ => $a{$_} "; } print "nn"; …
05_subs. pl sub greeting { my @param = @_; } print "Hello, $param[0], how are you? nn"; return 0; print "Round 1: n"; foreach (@names) { greeting($_); } Notes • 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 Notes my @names = ('Jeff', 'Jon', 'David', 'Sam'); • 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 objectoriented programming sub scope 1 { print "In Scope 1: n"; print "t. Names: @namesn"; } my @new_names = ('Ffej', 'Noj', 'Divad', 'Mas'); print "t. New Names: @new_namesn";
07_files. pl Notes sub read_file { my @params = @_; . /07_files. pl File 1. txt File 2. txt diff File 1. txt File 2. txt cp File 1. txt File 2 CP. txt diff file 2. txt File 2 CP. txt my $filename = $params[0]; open(FILE, '<', $filename) or die $!; … } … print "n. Read Filen"; read_file("File 1. txt"); print "n. Write Filen"; copy_file("File 1. txt", "File 2. txt"); • Read and write from files • Fundamental importance
08_echo. pl Notes #!/usr/bin/perl . /08_echo. pl This is a test echo This is a test use strict; use warnings; my $argc = scalar(@ARGV); print "@ARGVnn"; for (my $i=0; $i<$argc; $i++) { print "$ARGV[$i] "; } print "nn"; foreach (@ARGV) { print "$_ "; } print "n"; • Uses @ARGV builtin array
09_stats. pl Notes my $argc = scalar(@ARGV); # You can … . /09_stats. pl 1 2 3 4 5 print "@ARGVn"; 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: $argcn"; print "mean: $meann"; print "stddev: $stddevn"; • # denotes comments • Uses @ARGV builtin array • Single pass standard deviation
10_cat. pl Notes sub read_file { my @params = @_; . /10_cat. pl File 1. txt File 2. txt cat File 1. txt File 2. txt my $filename = $params[0]; open(FILE, '<', $filename) or die $!; while (my $line = <FILE>) { print $line; } } close(FILE); return 0; • Basic command line args • Basic file I/O
11_wc. pl Notes sub count_file { my @params = @_; . /11_wc. pl File 1. pl wc File 1. pl 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); } • Basic file I/O • Accumulator • String Split return "$characters $words $lines"; • Character, word, line order reversed
12_cut. pl Notes sub cut_file { my @params = @_; . /12_cut. pl File 1. csv cut –d , -f 1, 4 File 1. csv my $filename = $params[0]; open(FILE, '<', $filename) or die $!; while (my $line = <FILE>) { chomp($line); my @line_array = split(', ', $line); my $column 1 = $line_array[0]; my $column 4 = $line_array[3]; } print "$column 1, $column 4n"; close(FILE); } return 0; • Split on comma • Print selected columns
13_grep. pl while (my $line = <FILE>) { Notes. /13_grep. pl File 1. log chomp($line); if ($line =~ m/System Sleep/) { $times = $times + 1; my @line_array = split(' ', $line); my $date 1 = $line_array[0]; my $date 2 = $line_array[1]; my $time = $line_array[2]; } } print "System Sleep at: $date 1 $date 2 $timen"; • Chomp procedure • If/then control structure • Regular expression • Log file analysis
14_head. pl Notes sub read_file { my @params = @_; . /14_head. pl File 1. log head File 1. log 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; • Use of boolean and in while • Failed use of break • Common use of comments
15_awk. pl Notes sub hist_file { my @params = @_; • . /15_awk. pl File 2. csv 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]; • AWK is actually is own programming • String eq not =
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
- Slides: 24