in reply to How can I run only part of a script determined by a posted variable?

You can use a hashtable lookup of closures (aka: code refs)

This would let you build up the hashtable in any order you want, possibly adding to it in multiple files (maintained by different people), possibly refering to methods / coderefs written by other people, in other packages.

Understanding how the example below works is left as an excersize fo the reader.
(hint: look at perldoc perlref)

#!/usr/local/bin/perl -wl use strict; my $function = $ARGV[0]; my $input_file = $ARGV[1];; sub create_file { my $file = shift; open (NETH, ">$file"); #actions print "Creating $file"; close (NETH); } my %function_registry = ( 'open' => sub { my $file = shift; open (BLAH, "$file"); #actions print "Opening $file"; close (BLAH); }, 'create' => \&create_file ); &{$function_registry{$function}}($input_file); __END__ [hossman@laptop ~]$ monk.pl create /tmp/file Creating /tmp/file [hossman@laptop ~]$ monk.pl open /tmp/file Opening /tmp/file

Replies are listed 'Best First'.
Re: Answer: How can I run only part of a script determined by a posted variable?
by hossman (Prior) on Jan 15, 2002 at 13:33 UTC
    Can't seem to update my answer, but I should have mentioned: This approach is very similar to the Command Design Pattern