# Try and catch using prototypes. (Almost verbatim from perlsub.)
sub try (&$) {
my ($try, $catch) = @_;
eval { &$try; };
if ($@) {
local $_ = $@;
&$catch;
}
}
sub catch (&) {
$_[0]
}
try {
die "phooey";
} catch {
/phooey/ and print "unphooey\n";
};
####
# Nested subroutines.
sub frobnicate {
my $input = shift;
local *grok = sub {
my $input = shift;
# grok's $input is lexical, and therefore
# different from the outer $input.
return $input . " grokked!";
}; # Note the semicolon.
return grok($input);
}
print frobnicate('foo'), "\n";
# However, this will work as well.
sub frobnicate_again {
my $input = shift;
local *grok = sub { return $input . " grokked!"; };
return grok($input);
}
print frobnicate_again('foo'), "\n";
##
##
my $ip = 127.0.0.1;
printf "%vd\n", $ip;