- Example Program 1
#!/usr/bin/perl
#filename perl_example1.pl
# Version 1
print "Version 1\n";
while ($a=<STDIN>)
{ chomp($a); # chomp removes the carriage return/line feed if there is one
print "Hello $a\n";
if ($a eq "EXIT") # use lt le eq ne gt ge for strings < <= == != > >= for numeric
{ last;
}
}
print "That's all folks\n";
# Version 2
print "Version 2\n";
while (chomp($a=<>)) # Note that leaving a blank filehandle <> results in the standard input
{ print "Hello $a\n";
if ($a =~ m/EXIT/i) # the match operator. Very useful...note here we are ignoring case
{ last;
}
}
# Version 3
print "Version 3\n"; # Compare this version with version 4
while(chomp($_=<>))
{ print "$_\n";
if ($_=~ m/EXIT/i)
{ last;
}
}
# Version 4
print "Version 4\n";
while(<>) # There appears to be a missing variable. $_ is the default variable!!
{ chomp;
print ;
if (m/EXIT/i)
{ last;
}
}
print "That's it folks\n";
- Example Program 2 (if , if else, if elsif else, for, while)
#!/usr/bin/perl
#filename perl_example2.pl
# Note indentation!!!!
#Sample statements
$a=23;
$b=56;
if ($a < $b )
{ print "a is less than b\n";
}
if ($b<$a)
{ print "b is less than a\n";
}
else
{ print "a is less than b\n";
}
$c=77;
if ( ($a<$b) && ($a<$c))
{ print "a is the smallest\n";
}
elsif ( ($b<$a) && ($b<$c))
{ print "b is smallest\n";
}
else
{ print "C is smallest\n";
}
$a=1;
$sum=0;
while ($a<=10)
{ $sum+=$a++;
}
print "The sum is $sum\n";
for($a=1;$a<=10;$a++)
{ $sum+=$a;
}
print "The sum is $sum\n";
- Arrays in perl
#!/usr/bin/perl -w
use strict;
# Introduction to arrays
{
my @ar;
$ar[0]='harry';
$ar[1]='tome';
$ar[22]='mary';
# $#ar will report the highest subscript of the array
# Treating the array like a scalar value as in :
# $b=@ar
# will retunr the number of elements in the array
# (including undefined elements)
for($a=0;$a<=$#ar;$a++)
{ if (defined($ar[$a]))
{ print "$ar[$a] found at $a\n";
}
}
# A new construct: foreach
my $item;
foreach $item( @ar)
{ if (defined($item))
{ print "Element is $item\n";
}
}
print "\n\n";
my @ar2;
@ar2=('bobo','phineas','alicia','per','heinrich');
# Sort an Array. Note that the sort command returns a sorted
# array, it does *not* change the original array
foreach $item (sort @ar2)
{ if (defined($item))
{ print "Element is $item\n";
}
}
# The reverse command. Like sort, it returns a copy of the array
# in reverse order, it does not change the original array
print "\n\n";
foreach $item (reverse @ar2)
{ print "Element is $item\n";}
}
-
Lists in perl
#!/usr/bin/perl -w
use strict;
# LISTS
# Here's a perl list:
#('cat','dog','mouse');
# The preceeding does not do much !!! But we can assign an array to a list
my @ar;
@ar=('cat','dog','mouse');
# Likewise, we can assign elements of a list from an array
my $pet1;
my $pet2;
my $pet3;
($pet1,$pet2,$pet3)=@ar;
# push/pop and shift/unshift operators
# we can add/delete elements from the **end** of an array with push/pop
my $x;
$x=pop(@ar); # $x has value mouse and @ar=('cat','dog')
push(@ar,56); # @ar=('cat','dog',56)
$x=shift(@ar); # $x='cat' @ar=('dog',56)
unshift(@ar,99); # @ar=(99,'dog',56)
-
Simple subroutines
#!/usr/bin/perl -w
# Introduction to subroutines
# Filename: perlsubs.pl
# Example 1 - no arguments, everything global
$a=23;
&sub_it; # use ampersand for subroutine
print "\$a is $a\n";
$x=&sub_it;
print "Returned value is $x\n";
exit;
# Subroutines always return the last executable value
sub sub_it
{
$a=$a+5;
56;
}
- Subroutines with parameters
#!/usr/bin/perl
# Subroutines with parameters
# Filename: perlsubs2.pl
# Problem: find the maximum value of a set of integers
$big=&maxit(12,45,78,-2314.5);
print "Maximum value is $big\n";
$big=&maxit(-34,5);
print "Maximum value is $big\n";
$big=&maxit(23);
print "Maximum value is $big\n";
# IMPORTANT: parameters are passed to a subroutine via the array
# called @_ . Elements of this array are $_[0],$_[1], etc......
sub maxit
{
my @data=@_;
my $size=@data;
my $largest=$data[0];
for($a=1;$a<$size;$a++)
{ if ($data[$a]>$largest)
{ $largest=$data[$a];
}
}
$largest;
}
- Associative Arrays
#!/usr/bin/perl -w
# Hashes (or associative arrays)
# Similar to arrays, but indexing may be performed by strings
# Filename: perlhash.pl
%hash_1=("name","henry","age",23,"sex","male");
# keys and values functions:
@sk=sort(keys %hash_1);
# Note: the following gives unformatted output
print @sk;
#nice output
print "\n";
foreach $item(sort keys %hash_1)
{ print "Value of $item is $hash_1{$item}\n";}
#add to hash
$hash_1{"title"}="Mr";
foreach $item(sort keys %hash_1)
{ print "Value of $item is $hash_1{$item}\n";}
# The BIG Arrow (or another way to initialize hashes
%hash_2 =
("lastname" => "jones",
"firstname" => "sue",
"id" => 5123457,
"class" => "junior", #Note: comma is not an error
);
print "\n\n";
foreach $item(sort keys %hash_2)
{ print "$item => $hash_2{$item}\n";}
# Another unusual loop construct
print "\n\n";
while ( ($key,$value) = each %hash_2)
{ print "$key => $value\n"; }