Log in Page Discussion History Go to the site toolbox

Markrh:PerlProgramming:ReferencesToFunctions

From BluWiki

Code, fnRefs.pl

#!/usr/local/bin/perl -w
use strict;

my %fnRef;
$fnRef{'Chicago'}    = \&fnChi;
$fnRef{'Cincinnati'} = \&fnCin;
$fnRef{'Houston'}    = \&fnHou;
$fnRef{'Pittsburgh'} = \&fnPit;
$fnRef{'St. Louis'}  = \&fnStL;

my $city = $ARGV[0];
my $msg  = $ARGV[1];

$fnRef{$city}->($msg) if (defined($fnRef{$city}));
print "huh?\n" unless(defined($fnRef{$city}));

# ----------------------------------------------- fnChi
sub fnChi {
  my $msg = shift;

  print "Cubs. $msg\n";
}

# ----------------------------------------------- fnCin
sub fnCin {
  my $msg = shift;

  print "Reds. $msg\n";
}

# ----------------------------------------------- fnHou
sub fnHou {
  my $msg = shift;

  print "Astros. $msg\n";
}

# ----------------------------------------------- fnPit
sub fnPit {
  my $msg = shift;

  print "Pirates. $msg\n";
}

# ----------------------------------------------- fnStL
sub fnStL {
  my $msg = shift;

  print "Cardinals. $msg\n";
}

Output

./fnRefs.pl Cincinnati "Kilroy was here"
Reds. Kilroy was here

Perl Programming, Cookbook

Site Toolbox:

Personal tools
GNU Free Documentation License 1.2
This page was last modified on 8 January 2009, at 04:29.
Disclaimers - About BluWiki