Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: How can I run only part of a script determined by a posted variable?

by hossman (Prior)
on Jan 15, 2002 at 13:23 UTC ( [id://138858]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://138858]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-03-29 14:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found