Perl 5 Intro Tobias Oetiker OETIKER+PARTNER AG November 24, 2014
What is Perl? ◮ Practical Extraction and Report Language ◮ Many Platforms ◮ Powerful Text Manipulation ◮ Web Programming ◮ There is more than one way to do it
Course Documentation ◮ Booklet: Perl Programming ◮ 15 Sections ◮ Most Sections have Exercises ◮ Bonus Exercises if you have time
A basic program #!/ usr/bin/env perl 1 use 5.010; 2 use strict; 3 use warnings; 4 use diagnostics; 5 say ’Hello world .’; 6 ◮ perldoc ◮ perldoc -f print ◮ perldoc Perl::Module ◮ perl -E ’say "Hello world"’
Running the program ◮ chmod +x progname ◮ ./progname or just progname if in your PATH ◮ perl -e ’program code’
3 - Scalar variables my $x; 1 $x = "some text "; 2 $x = 444; 3 $x = "444"; $x++; 4 5 say ’the value of \$x is $x ’; 6 say "the value of \$x is $x"; 7
Array variables my @rgb = ("red", "green", "blue "); 1 say "First RGB component: $rgb [0]"; 2 say "All components: @rgb "; 3 say "Number of components: ", scalar @rgb;, 4
File handling my $file = ’/etc/passwd ’; # Name the file 1 open my $info , $file or # Open the file 2 die "Can ’t open $file: $!\n"; 3 my @lines = <$info >; # Read it into an array 4 close $info; # Close the file 5 print @lines; # Print the array 6
File handling (2) open(my $cmd , "who |") or die "Can ’t run who: $!\n"; 1 while(<$cmd >) { # $_ 2 print; 3 } 4
Control structures ◮ Usual for , while and until ◮ foreach is in reality synonim of for ◮ for my $a (@list) { block; } ◮ for (my $a=0; $a<10; $a++) { block; }
Conditionals if (!$a) { 1 print "The string is empty\n"; 2 } 3 elsif (length($a) == 1) { 4 say "The string has one character "; 5 } 6 elsif (length($a) == 2) { 7 say "The string has two characters "; 8 } 9 else { 10 say "The string has lots of characters "; 11 } 12 ◮ /etc/mailcap
Hashes my %david = ( 1 last_name => "Tobi", 2 first_name => "Oetiker", 3 email => ’tobi@oetiker.ch ’, 4 ); 5 6 $david{phone} = ’27019 ’; 7 8 print "david ’s email: $david{email }\n"; 9
References my @colors = ( ’red ’, ’green ’, ’blue ’ ); 1 my $colors_ref = \@colors; 2 $colors_ref = [ ’red ’, ’green ’, ’blue ’ ]; 3 4 my @colors_copy = @{$colors_ref }; # @colors 5 my $first_color = ${$colors_ref }[0]; # $colors [0] 6 $first_color = $colors_ref - >[0]; 7
String matching my $text = "quick brown fox"; 1 2 $text =˜ /quick/ # quick anywhere in $text ? 3 $text =˜ /ˆ quick/ # quick at beginning of $text 4 $text =˜ /quick .*fox/ # quick - something - fox 5 $text =˜ /quick/i; # case insensitive 6 7 $text =˜ /quick (.*) fox/ and print "<$1 >\n"; 8 9 < brown > 10 11 $text =˜ /\/ match \/a\/ path /; 12 $text =˜ m|/ match/a/path |; 13 $text =˜ m#/ match/a/path #; 14 $text =˜ m{/ match/a/path }; 15
Substitution my $text = "quick brown fox"; 1 2 $text =˜ s/brown/blue /; 3 $text =˜ s#brown#blue #; 4 $text =˜ s{brown }{ blue }; 5 6 $text =˜ s/quick (.*) fox/fox$1quick /; 7
Split my $info = "Caine : Michael: Actor :14 - Leafy Drive "; 1 2 my @personal = split (/\s*:\s*/, $info ); 3 4 print "<$personal [2] >\n"; # -> <Actor > 5
Subroutines sub match { 1 my ($a , $b) = @_; 2 return $a =˜ $b; 3 } 4 5 match(’brown fox ’, ’fox ’); 6 Fibonacci: a 0 = 1, a 1 = 1, a n = a n − 2 + a n − 1
14 - Variable scope $x = 1; # global variable 1 2 # $x: 1 3 sub a { 4 # $x: 1 5 my $x = 2; 6 # $x: 2 7 if(sometest) { 8 # $x: 2 9 my $y = 3; 10 # $y: 3 11 } 12 # $x: 2 13 # $y: undefined 14 } 15 # $x: 1 16
Installing Perl Modules $ export PERL5LIB =˜/ perl5/lib/perl5 1 $ export PATH =˜/ perl5/bin:$PATH 2 $ curl -L http :// cpanmin.us |\ 3 perl - -n -l ˜/ perl5 App:: cpanminus 4 $ cpanm -l ˜/ perl5 Mojolicious 5
Recommend
More recommend