perl programming language

Learn from examples

1. Program to output a string, integer, and floating point expl1.pl source code # expl1.pl example print string, integer, float # optional use statements use strict; use warnings; use diagnostics; my $stra = "a string"; my $int13 = 13; my $flt1 = 31.75; my $flt2 = 1.23e+30; print "perl expl1.pl running\n"; print "stra= $stra \n"; print "int13= $int13 \n"; print "first float = $flt1 \n"; print "big float = $flt2 \n"; print "expl1.pl finished\n"; Output from execution: perl expl1.pl running stra= a string int13= 13 first float = 31.75 big float = 1.23e+30 expl1.pl finished 2. commands to execute the source code at a minimum, Windows, Linux, MacOSX. Windows: perl expl1.pl Linux: perl expl1.pl MacOSX perl expl1.pl Sample Makefile: all: expl1.out expl1.out: expl1.pl perl expl1.pl ≷& expl1.out cat expl1.out 3. You must be able to declare variables and arrays and matrix of various types. expl3.pl source code # expl3.pl example declare and print variable, vector, matrix use strict; use warnings; print "perl expl3.pl running \n"; my $var1 = 17; print "var1= $var1 \n"; my @arr1 = (2, 4, 6); print "arr1= @arr1 \n"; print "arr1[0] = $arr1[0] \n"; my @mat2 = ([1, 2], [3, 4]); print "mat2 $mat2[0][0] $mat2[0][1] \n"; print " $mat2[1][0] $mat2[1][1] \n"; print " \n"; print "mat2= @mat2 \n"; # not useful print "expl3.pl finished\n"; Execution output: perl expl3.pl running var1= 17 arr1= 2 4 6 arr1[0] = 2 mat2 1 2 3 4 mat2= ARRAY(0x562f331d0c78) ARRAY(0x562f331d0d50) expl3.pl finished 4. You need to be able to have loops, iteration statements expl4.pl source code # expl4.pl example loops, iteration use strict; use warnings; print "perl expl4.pl running\n"; my $n = 3; my $sum = 0; for (my $i = 0; $i < $n; $i++) { $sum += $i; print "i= $i sum = $sum \n"; } my $k = -1; while($k < $n) { print "k= $k \n"; $k += 2; } foreach my $i (1..3) { foreach my $j (4..5) { print "i,j $i $j \n"; } } my $min = 999; foreach my $item (5, 2, 1, 4, 3) { $min = $item if $min ≷ $item; } print "Min = $min \n\n"; print "expl4.pl finished\n"; Execution output: perl expl4.pl running i= 0 sum = 0 i= 1 sum = 1 i= 2 sum = 3 k= -1 k= 1 i,j 1 4 i,j 1 5 i,j 2 4 i,j 2 5 i,j 3 4 i,j 3 5 Min = 1 expl4.pl finished 5. You need if then else conditional statements expl5.pl source code # expl5.pl example of if then else # expl5.pl example if then else use strict; use warnings; print "perl expl5.pl running\n"; my $k = 3; if ($k ≷ 1) { print "k= $k \n"; } my @lst = ( 2, 4, 6, 8 ); if($lst[0] < 2) { print "no \n"; } else { print "lst[0] = $lst[0] \n"; } my $i = 4; if ($i ≤ 3) { print "i ≤ 3 \n"; } elsif ($i ≥ 5) { print "i ≥ 5 \n"; } elsif ($i == 3) { print "elsif i==3 \n"; } else { print "else \n"; } print "expl5.pl finished\n"; Execution output: perl expl5.pl running k= 3 lst[0] = 2 else expl5.pl finished 6. You need to be able to create functions, procedures, subroutines. expl6.pl source code # expl6.pl example create and call function and procedure "sub" use strict; use warnings; print "perl expl6.pl running\n"; my $a = 1.1; my $b = 3.3; my $ab = add($a, $b); print "a+b using add = $ab \n\n"; my @mat1 = ([1, 2, 3], [4, 5, 6]); print "mat1= [$mat1[0][0], $mat1[0][1], $mat1[0][2]], \n"; print " [$mat1[1][0], $mat1[1][1], $mat1[1][2]] \n"; my $nrow1 = @mat1; my $ncol1 = @{$mat1[0]}; print "nrow1= $nrow1, ncol1= $ncol1 \n"; print " \n"; my @mat2 = ([10, 11, 12], [13, 14, 15]); print "mat2= [$mat2[0][0], $mat2[0][1], $mat2[0][2]], \n"; print " [$mat2[1][0], $mat2[1][1], $mat2[1][2]] \n"; print " \n"; my @sum = addmat(\@mat1, \@mat2); print "sum = [$sum[0][0], $sum[0][1], $sum[0][2]], \n"; print " [$sum[1][0], $sum[1][1], $sum[1][2]] \n"; print " \n"; print "expl6.pl finished\n"; sub add { my ($arg1, $arg2) = @_; # value parameters return $arg1 + $arg2; } # end add sub addmat { my ($arg1, $arg2) = @_; # array parameters my @a1 = @{$arg1}; # copy, dereferenced my @a2 = @{$arg2}; my $nr = @a1; my $nc = @{$a1[0]}; print "nr= $nr, nc= $nc \n"; my @suma; # matrix example for(my $i=0; $i < $nr; $i++) { for(my $j=0; $j < $nc; $j++) { $suma[$i][$j] = $a1[$i][$j]+$a2[$i][$j]; } } return @suma; } # end addmat Execution output: perl expl6.pl running a+b using add = 4.4 mat1= [1, 2, 3], [4, 5, 6] nrow1= 2, ncol1= 3 mat2= [10, 11, 12], [13, 14, 15] nr= 2, nc= 3 sum = [11, 13, 15], [17, 19, 21] expl6.pl finished 7. You need to be able to read and write files in various formats. expl7.pl source code # expl7.pl example read and write files use strict; use warnings; print "perl expl7.pl running\n"; print "perl expl7.pl aa bbb cccc < expl7.dat > expl7.out"; my $num_args = $#ARGV + 1; my $filename = 'cmd_pl.txt'; open(my $fh, '<', $filename) or die "Could not open file '$filename' $!"; print $fh "args from cmp.pl \n"; print "num_args=$num_args \n"; print $fh "num_args=$num_args \n"; for(my $i=0; $i < $num_args; $i++) { print "$ARGV[$i] \n"; print $fh "$ARGV[$i] \n"; } print "STDIN is: \n"; print $fh "STDIN from expl7.dat \n"; foreach my $line ( < STDIN > ) { chomp( $line ); print "$line \n"; print $fh "$line \n"; } print "\n"; print $fh "end of STDIN\n"; close $fh; print "expl7.pl finished\n"; Using: expl7.dat that has: red green blue Execution output: perl expl7.pl running perl expl7.pl aa bbb cccc < expl7.dat >: expl7.outnum_args=3 aa bbb cccc STDIN is: red green blue expl7.pl finished 8. You need to be able to use a number of files combined to build a program. This may include packages, libraries, operating system commands, header files, etc. expl8m.pm module source code package expl8m; # expl8m.pm package for expl8.pl use strict; use Exporter qw(import); our $VERSION = 1.00; our @ISA = qw(Exporter); our @EXPORT_OK = qw(matadd matsub); # see matrix.pm for many subroutines sub matadd { my ($mat1, $mat2, $sum) = @_; # parameters my $r1 = @$mat1; my $c1 = @{$mat1->[0]}; my $r2 = @$mat2; my $c2 = @{$mat2->[0]}; print "in matadd r1=$r1, c1=$c1, r2=$r2, c2=$c2 \n"; for (my $i = 0; $i *lt; $r1; $i++) { for (my $j = 0; $j < $c1; $j++) { $sum->[$i][$j] = $mat1->[$i][$j] + $mat2->[$i][$j]; } } } # end matadd sub matsub { my ($mat1, $mat2, $sum) = @_; # parameters my $r1 = @$mat1; my $c1 = @{$mat1->[0]}; my $r2 = @$mat2; my $c2 = @{$mat2->[0]}; print "in matsub r1=$r1, c1=$c1, r2=$r2, c2=$c2 \n"; for (my $i = 0; $i < $r1; $i++) { for (my $j = 0; $j < $c1; $j++) { $sum->[$i][$j] = $mat1->[$i][$j] - $mat2->[$i][$j]; } } } # end matsub 1; expl8.pl source code # expl8.pl example use external functions, procedures, subroutines # uses expl8m.pm in local directory use strict; print "perl expl8.pl running \n"; use my::expl8m qw(&matadd &matsub ); # could be many more my @mat1 = ([1, 2], [3, 4], [5, 6]); my @mat2 = ([10, 11, 12], [13, 14, 15]); my @mat3 = ([2, 3], [4, 5], [6, 7]); my @sum = ([0, 0], [0, 0], [0, 0]); print "print mat1, mat2, mat3 \n"; matprt("mat1", \@mat1); print "\n"; matprt("mat2", \@mat2); print "\n"; matprt("mat3", \@mat3); print "\n"; matadd(\@mat1, \@mat3, \@sum); print "matadd sum= \n"; matprt("sum", \@sum); print "\n"; my @dif; matsub(\@mat3, \@mat1, \@dif); print "matsub \@ dif= \n"; matprt("dif", \@dif); print "\n"; print "expl8.pl finished\n"; Execution output: perl expl8.pl running print mat1, mat2, mat3 in matprt nrow=3, ncol=2 mat1 [ 0 ] [ 0 ] = 1 mat1 [ 0 ] [ 1 ] = 2 mat1 [ 1 ] [ 0 ] = 3 mat1 [ 1 ] [ 1 ] = 4 mat1 [ 2 ] [ 0 ] = 5 mat1 [ 2 ] [ 1 ] = 6 in matprt nrow=2, ncol=3 mat2 [ 0 ] [ 0 ] = 10 mat2 [ 0 ] [ 1 ] = 11 mat2 [ 0 ] [ 2 ] = 12 mat2 [ 1 ] [ 0 ] = 13 mat2 [ 1 ] [ 1 ] = 14 mat2 [ 1 ] [ 2 ] = 15 in matprt nrow=3, ncol=2 mat3 [ 0 ] [ 0 ] = 2 mat3 [ 0 ] [ 1 ] = 3 mat3 [ 1 ] [ 0 ] = 4 mat3 [ 1 ] [ 1 ] = 5 mat3 [ 2 ] [ 0 ] = 6 mat3 [ 2 ] [ 1 ] = 7 in matadd r1=3, c1=2, r2=3, c2=2 matadd sum= in matprt nrow=3, ncol=2 sum [ 0 ] [ 0 ] = 3 sum [ 0 ] [ 1 ] = 5 sum [ 1 ] [ 0 ] = 7 sum [ 1 ] [ 1 ] = 9 sum [ 2 ] [ 0 ] = 11 sum [ 2 ] [ 1 ] = 13 in matsub r1=3, c1=2, r2=3, c2=2 matsub @ dif= in matprt nrow=3, ncol=2 dif [ 0 ] [ 0 ] = 1 dif [ 0 ] [ 1 ] = 1 dif [ 1 ] [ 0 ] = 1 dif [ 1 ] [ 1 ] = 1 dif [ 2 ] [ 0 ] = 1 dif [ 2 ] [ 1 ] = 1 expl8.pl finished See full implmintation of matrix operations: matrix.pm source code test_matrix.pl source code test_matrix_pl.out output

  Last updated 8/15
  /2019