in reply to Call subroutine by reference ?

The canonical solution to this issue is to use a "Dispatch table".

This is typically a hash whose keys are the text that is used to identify which sub to call, and the value is a reference to the sub.

my %Dispach_table = ( UNO => \&Process_One, DOS => \&Process_Two, TRES => sub { print "This is an example of an in-line subref\n"}, ); sub Process_One{ #whatever } sub Process_Two{ # Some code } my $var = "DOS"; # Invoke the dispatch (after verifying it exists) $Dispatch_table{ $var } or die "ERROR: attempt to dispatch non-existin +g entry: '$val'"; $Dispatch_table{ $var } -> ( #Parameters, if any ); # Here is where the routine gets called.
Update: Added check for "existence" as recommended by sundialsvc4 (++), below. (Also fixed text typo).

             "I'm fairly sure if they took porn off the Internet, there'd only be one website left, and it'd be called 'Bring Back the Porn!'"
        -- Dr. Cox, Scrubs

Replies are listed 'Best First'.
Re^2: Call subroutine by reference ?
by locked_user sundialsvc4 (Abbot) on May 31, 2013 at 10:48 UTC

    Yes, and to clarify with-regard-to my previous post, there is no conflict between the two ideas ... they are quite-ordinarily used together, as Perl applications are quite-ordinarily built as systems of packages instead of monolithic and repetitive individual scripts.   If you use a package, the subs in them are (ordinarily ...) visible, both as subroutine_name and, if necessary, as package_name::subroutine_name.   You can build code-references freely, as shown, because everything comes-together in memory (just...) before execution of the whole thing begins.   The dispatch-table idea obviously is clearer and more-expandable than my if..elsif.. structure, which was merely used here for clarity.

    n.b.:   In actual practice, the exists() function should be used to verify that the key being sought actually does exist in the table, before attempting the call.   e.g. die "horribly" unless exists($dispatch_table->{$key}); ... so that the program will react graciously, or at-least die on its own terms instead of puking with “can't use 'undef' as a code reference” or what-not.

Re^2: Call subroutine by reference ?
by JockoHelios (Scribe) on May 31, 2013 at 17:09 UTC

    The Dispatch Table appeals to the Organization-OCD side of my brain :)

    I might end up with more than one main script, depending on the modifications I make to the analysis subroutines due to some variations on data content. So I'll need to pay more attention than usual to the code variations.

    Fortunately, I like that kind of project ;)

    Dyslexics Untie !!!