lecture
in color
{'
}'
return to return values.
sort, take an
argument that's a block.
sort { $a cmp $b } (@thing); Sort in alphabetical order.
sort { $a <=> $b } (@thing); Sort in numeric order.
map:
map { $_ . 'lb' } (1,3,5,7);
results in the array
('1lb', '3lb', '5lb', '7lb')
{' and '}' have another meaning when
on the RHS of '='(associative array literal).
contents of perl_subs/e02.cgi...
#! /var/local/couch/bin/perl
print "Content-type: text/plain\n\n";
# examples of blocks
# blocks are required when iteration is written with
# condition first.
foreach $thing (1,2,3,4) {
print "$thing\n";
}
# can open a new block anywhere one wishes.
{
print "inside a new block\n";
}
# some subroutines have an optional first argument that's a block
# default sort is ALPHABETIC
@alpha = sort(1,12,23,41,2);
$alpha = join(',',@alpha);
# can change this to NUMERIC with <=> operator
@numeric = sort { $a<=>$b } (1,12,23,41,2);
$numeric = join(',',@numeric);
print "\@alpha=$alpha, \@numeric=$numeric\n";
# blocks mean something completely different when on RHS of =
$i = { $j = 15 };
print '$i = { $j = 15 };'; print "\n";
print "\$i = $i, \$j = $j\n";
...end of perl_subs/e02.cgi
sub marine {
print "I hate boats\n";
}
&marine; - calls the subroutine.
@_
@_ are references to
arguments and one can change the arguments in the calling function by
setting the values of $_[0], $_[1], etc.
@_ under the rules of syntax!
sub standard {
($name, $id) = @_;
print "you, $name, id $id, are quite silly.\n";
}
&standard('Couch', 20);
shift builtin function
is @_ So we can write the above equivalently as:
sub standard {
$name = shift;
$id = shift;
print STDERR "you gave too many arguments!\n" if @_ != 0;
print "you, $name, id $id, are quite silly.\n";
}
&standard('Couch', 20);
if @_ != 0: forces @_ into scalar context,
is true if the length of @_ isn't 0, i.e.,
there are more arguments to shift!
contents of perl_subs/e03.cgi...
#! /var/local/couch/bin/perl
print "Content-type: text/plain\n\n";
# fun with subroutines
sub tellme {
$args = scalar(@_);
print "I got $args arguments!\n";
for ($i=0; $i<@_; $i++) {
print "argument $i is $_[$i]\n";
}
print "\n";
}
&tellme('hi');
&tellme('hi','ho','hi','ho','it\'s off', 'to work', 'we go');
@stuff = ('I', 'am', 'here');
&tellme(@stuff);
&tellme(@stuff,@stuff);
&tellme(1,2,3,@stuff);
&tellme(('extra'),((('parens','don\'t'))), 'matter');
...end of perl_subs/e03.cgi
contents of perl_subs/e05.cgi...
#! /var/local/couch/bin/perl
# typical use of subroutines
&httpHeader;
&htmlStart;
&h1("Properties of breakfast cereals");
&ulStart;
&li('tastes like cardboard');
&li('is made of cardboard');
&li('is a byproduct of making cardboard');
&ulEnd;
&code("This is <html> .. </html> code\n");
&htmlEnd;
exit 0;
sub httpHeader {
print "Content-type: text/html\n\n";
}
sub escapeHTML {
my $string = shift; # statically scoped; no reason for dynamic
$string =~ s/&/\&/g;
$string =~ s/</\</g;
$string =~ s/>/\>/g;
return $string;
}
sub code {
my ($thing) = @_; # statically scoped
print "<CODE>\n";
print &escapeHTML($thing);
print "</CODE>\n";
}
sub li {
my $thing = shift;
print "<LI>\n";
print $thing;
print "</LI>\n";
}
sub h1 {
my $thing = shift;
print "<H1>\n";
print $thing;
print "</H1>\n";
}
sub ulStart { print "<UL>\n"; }
sub ulEnd { print "</UL>\n"; }
sub htmlStart {
my $title = shift;
print <<EOF
<HTML>
<HEAD>
<TITLE>$title</TITLE>
</HEAD>
<BODY>
EOF
;
}
sub htmlEnd {
print <<EOF
</BODY>
</HTML>
EOF
;
}
...end of perl_subs/e05.cgi
Try this out.
contents of perl_subs/e06.cgi...
#! /var/local/couch/bin/perl
# packages:
print &render::generate;
exit 0;
package render;
sub generate {
&http(
&html('Title',
&h1('Title'),
&ul(
&li('item 1'),
&li('item 2'),
&li('item 3')
)
)
);
}
sub http {
return "Content-type: text/html\n\n" . join('',@_);
}
sub escapeHTML {
my $string = shift; # statically scoped; no reason for dynamic
$string =~ s/&/\&/g;
$string =~ s/</\</g;
$string =~ s/>/\>/g;
return $string;
}
sub code {
my $txt = "<CODE>\n";
my $thing;
foreach $thing (@_) {
$txt .= &escapeHTML($thing);
}
$txt .= "</CODE>\n";
return $txt;
}
sub li { "<LI>\n" . join('',@_) . "</LI>\n" }
sub h1 { "<H1>\n" . join('',@_) . "</H1>\n"; }
sub ul { "<UL>\n" . join('',@_) . "</UL>\n"; }
sub ol { "<OL>\n" . join('',@_) . "</OL>\n"; }
sub html {
my $title = shift;
return <<EOF
<HTML>
<HEAD>
<TITLE>$title</TITLE>
</HEAD>
<BODY>
EOF
. join('',@_) . <<EOF
</BODY>
</HTML>
EOF
;
}
...end of perl_subs/e06.cgi
Try this out.
@_ rather
than exploding it into variables.
lecture
in color