bradcathey has asked for the wisdom of the Perl Monks concerning the following question:
Fellow Monasterians,
I've searched the "Internets" and the library here in the monastery, but can't find anything definitive. Read lots of stuff about dispatch tables and anonymous subs, and tried several approaches but epic fails. Here's what I'm trying to do. Thoughts? TIA.
my $sub = 'do_this'; print $sub("Fred"); sub do_this { my $var = shift; return "My name is $var"; } sub do_that { my $var = shift; return "My name is not $var"; } My name is Fred
UPDATE: I start getting nervous making exceptions to strict to make things work below, but more importantly, to kennethk's point (below) I might want to rethink why I'm using a variable for a variable name. I had tried to do this many years ago for simple scalars, but got a lesson from one of the senior monks at the time about using hashes, and it changed everything. So, a simple conditional statement or dispatch table might be a better practice.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Is it possible to use a scalar to call a subroutine?
by kennethk (Abbot) on May 06, 2010 at 16:55 UTC | |
by JavaFan (Canon) on May 06, 2010 at 17:32 UTC | |
by kennethk (Abbot) on May 06, 2010 at 17:39 UTC | |
by bradcathey (Prior) on May 06, 2010 at 18:32 UTC | |
Re: Is it possible to use a scalar to call a subroutine?
by BrowserUk (Patriarch) on May 06, 2010 at 16:56 UTC | |
Re: Is it possible to use a scalar to call a subroutine?
by originalgeek (Initiate) on May 06, 2010 at 17:10 UTC | |
Re: Is it possible to use a scalar to call a subroutine?
by biohisham (Priest) on May 06, 2010 at 18:23 UTC | |
Re: Is it possible to use a scalar to call a subroutine?
by rovf (Priest) on May 07, 2010 at 07:34 UTC |