Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
There are a huge number of ways to do this. They all boil down to:
my @values = qw(Stuff you want to pass in); print "Some question\n"; chomp(my $file = <>); if ($file eq 'A') { do_A($file, @values); } elsif ($file eq 'B') { do_B($file, @values); } elsif ... { } else { complain_that_value_sucks($file, @values); }

Now, that's a really piss-poor thing, especially if you have more than 5-10 options. So, we can use a dispatch table. This is a preferred method of doing things.

my %dispatch_table = ( A => \&do_A, B => \&do_B, C => \&do_C, ); my @values = qw(Stuff you want to pass in); print "Some question\n"; chomp(my $file = <>); my $sub = $dispatch_table{$file} || \&complain_that_value_sucks; $sub->($file, @values);

Now, the dispatch table can be in a module, built on-the-fly, or whatever. What's happening here is that we're taking references to subroutines and associating them with strings. So, whenever we see a given string, we can map that to the given subroutine. This is very similar (but more powerful) to funcp's in C/C++.

------
We are the carpenters and bricklayers of the Information Age.

The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.


In reply to Re: Calling options via Sub Routines by dragonchild
in thread Calling options via Sub Routines by sunadmn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-25 23:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found