Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
"be consistent"
 
PerlMonks  

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

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

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


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 08: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
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.