Perl Sample Programs

Max subroutines

sub max {
	my($max_so_far) = shift @_;   # the first one is largest yet seen
	foreach (@_) {
		if( $_ > $max_so_far ) {
			$max_so_far = $_;
		}
	}
	return $max_so_far;
}
$max_val = &max(1,2,4,7,10,13,15);
print("Max value is $max_val\n");

foreach

foreach(1..10) {
   my($square) = $_ * $_;
   print "$_ squared is $square.\n";
}
foreach my $singer (qw/ carly pitbull lorde/) {
	print "singer is $singer\n";
}

sub list_number {
my($m, $n) = @_;
if ( $m < $n ) { # count upward $m..$n; } else { # count downward reverse $n..$m; } } @list = &list_number(5, 11); foreach(@list) { print(“number is $_\n”); } @list = &list_number(12, 3); foreach(@list) { print(“number is $_\n”); } [/perl]