#!/usr/bin/perl -w # # Example of using a hash to store code. # # Joel Franusic 2007 use strict; my %code = ( 'as_reversed' => sub { my @in = split //, shift; for (my $x = $#in; $x >= 0; $x--) { print $in[$x]; } print "\n"; }, 'as_html' => sub { my $in = shift; $in =~ s/\n/\/g; print '' . '' . 'example output' . '
' . $in . '
' . '' . "\n"; }, 'as_hashes' => sub { my $in = shift; $in =~ s/\S/#/g; print $in; }, ); if( $ARGV[0] && $code{$ARGV[0]}) { &{$code{$ARGV[0]}}(do {local $/; }); } else { print "Nope! Try giving me some STDIN and using one of these as an argument:\n"; foreach my $sub (keys %code) { print "\t$sub\n"; } }