Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Your suggestion was a good one IMHO, or at least very close to being good - look up dispatch tables and/or coderefs in super search and you should find something. They are a useful and elegant way of handling this kind of problem.

UPDATE: Look here for a small hint on the use of dispatch tables. Code also added below...

my $foo; my $bar; my @args; # Get $foo $bar and @args somewhere around here... my %despatch_table = ("one" => ( "alpha" => \&func_onealpha, "beta" => \&func_onebeta), "two" => ("alpha" => \&func_twoalpha, "beta" => \&func_twobeta)); my $answer; if ($answer = &${$despatch_table{$foo}}{bar}(@args)) { # Do something with $answer } else { # Bad values throw an error } sub func_onealpha { # Do stuff here } sub func_onebeta { # Do stuff here } sub func_twoalpha { # Do stuff here } sub func_twobeta { # Do stuff here }

You use the string values of $foo and $bar as keys to the hash, which contains references to subroutines (in this case named, but can be anonymous subs in the hash itself. This is a bit of a rigmarole to go through, but ends up being better to work with than a huge nest of if statements.

Elgon

It is better either to be silent, or to say things of more value than silence. Sooner throw a pearl at hazard than an idle or useless word; and do not say a little in many words, but a great deal in a few.

Pythagoras (582 BC - 507 BC)


In reply to Re: Perl/Unix case by Elgon
in thread Perl/Unix case by Ronnie

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 sharing their wisdom with the Monastery: (4)
As of 2024-04-20 02:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found