Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Using a variable to call a sub-routine... (http://www.velocityreviews.com/forums/t897020-using-a-variable-to-call-a-sub-routine.html)

Big Jay 03-15-2006 11:54 PM

Using a variable to call a sub-routine...
 
Hi all,

I'm trying to call a sub-routine based on the value of a variable.

More specifically, I'm developing a menu system, where each level of the
menu has it's own sub-routine, but for a generalized "back" function, I'm
trying to call the last menu based on a variable, something like:

$lastMenu = apiMenu;

&$lastMenu; #I want this to be the same as calling &apiMenu

Does this make any sense?

Any help is greatly appreciated!



A. Sinan Unur 03-16-2006 12:08 AM

Re: Using a variable to call a sub-routine...
 
"Big Jay" <jbredice@hotmail.com> wrote in
news:dP1Sf.1513$tN3.1066@newssvr27.news.prodigy.ne t:

> I'm trying to call a sub-routine based on the value of a variable.
>
> More specifically, I'm developing a menu system, where each level of
> the menu has it's own sub-routine, but for a generalized "back"
> function, I'm trying to call the last menu based on a variable,
> something like:
>
> $lastMenu = apiMenu;
>
> &$lastMenu; #I want this to be the same as calling &apiMenu


Use a hash table to map menu names to menu handlers.

Sinan

--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html


Big Jay 03-16-2006 12:15 AM

Re: Using a variable to call a sub-routine...
 
I think I'll try making a default menu functions sub-routine and pass it
arguments for things like last menu... That should do the trick.

"Big Jay" <jbredice@hotmail.com> wrote in message
news:dP1Sf.1513$tN3.1066@newssvr27.news.prodigy.ne t...
> Hi all,
>
> I'm trying to call a sub-routine based on the value of a variable.
>
> More specifically, I'm developing a menu system, where each level of the
> menu has it's own sub-routine, but for a generalized "back" function, I'm
> trying to call the last menu based on a variable, something like:
>
> $lastMenu = apiMenu;
>
> &$lastMenu; #I want this to be the same as calling &apiMenu
>
> Does this make any sense?
>
> Any help is greatly appreciated!
>




Matt Garrish 03-16-2006 01:35 AM

Re: Using a variable to call a sub-routine...
 

"Big Jay" <jbredice@hotmail.com> wrote in message
news:dP1Sf.1513$tN3.1066@newssvr27.news.prodigy.ne t...
> Hi all,
>
> I'm trying to call a sub-routine based on the value of a variable.
>
> More specifically, I'm developing a menu system, where each level of the
> menu has it's own sub-routine, but for a generalized "back" function, I'm
> trying to call the last menu based on a variable, something like:
>
> $lastMenu = apiMenu;
>
> &$lastMenu; #I want this to be the same as calling &apiMenu
>
> Does this make any sense?
>


They're called symrefs, and they're to be avoided. It's always better to use
a hash:

my %subs = ( apiMenu => \&apiMenu );
my $lastMenu = 'apiMenu';
$subs{$lastMenu}();

Matt



Tad McClellan 03-16-2006 02:15 AM

Re: Using a variable to call a sub-routine...
 
Big Jay <jbredice@hotmail.com> wrote:


> I'm trying to call a sub-routine based on the value of a variable.



That is what is known as a "dispatch table".

You'll need to learn a bit about references to implement
a dispatch table in Perl.

(the word "subroutine" is not hyphenated.)


> More specifically, I'm developing a menu system, where each level of the
> menu has it's own sub-routine, but for a generalized "back" function, I'm
> trying to call the last menu based on a variable, something like:
>
> $lastMenu = apiMenu;
>
> &$lastMenu; #I want this to be the same as calling &apiMenu



-------------------
#!/usr/bin/perl
use warnings;
use strict;

my %menu_funcs = ( # a dispatch table
Open => \&open_file,
Edit => \&edit_file,
Close => \&close_file,
);

foreach my $menu_choice ( 'Open', 'Edit', 'Close' ) {
$menu_funcs{ $menu_choice }->(); # or: &{ $menu_funcs{ $menu_choice } }
}

sub open_file { warn "open_file() got called\n" }
sub edit_file { warn "edit_file() got called\n" }
sub close_file { warn "close_file() got called\n" }
-------------------


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas

robic0 03-16-2006 02:42 AM

Re: Using a variable to call a sub-routine...
 
On Wed, 15 Mar 2006 20:15:09 -0600, Tad McClellan <tadmc@augustmail.com> wrote:

>Big Jay <jbredice@hotmail.com> wrote:
>
>
>> I'm trying to call a sub-routine based on the value of a variable.

>
>
>That is what is known as a "dispatch table".
>

Maybe Tad, you can explain "dispatch table" a phrase used
exclusively for NT drivers...
Expanding out of Perl Tad?

>You'll need to learn a bit about references to implement
>a dispatch table in Perl.
>

You might even have to learn NT driver developer package...
>(the word "subroutine" is not hyphenated.)
>
>
>> More specifically, I'm developing a menu system, where each level of the
>> menu has it's own sub-routine, but for a generalized "back" function, I'm
>> trying to call the last menu based on a variable, something like:

Your developing a "menu system" in Perl? Hey Perl is *not* Windows!!!!!!!!!!!!!
Take a Windows developer class from me. Do you know at least C ?
>>
>> $lastMenu = apiMenu;
>>
>> &$lastMenu; #I want this to be the same as calling &apiMenu

>
>
>-------------------
>#!/usr/bin/perl
>use warnings;
>use strict;
>
>my %menu_funcs = ( # a dispatch table
> Open => \&open_file,
> Edit => \&edit_file,
> Close => \&close_file,
>);
>
>foreach my $menu_choice ( 'Open', 'Edit', 'Close' ) {
> $menu_funcs{ $menu_choice }->(); # or: &{ $menu_funcs{ $menu_choice } }
>}
>
>sub open_file { warn "open_file() got called\n" }
>sub edit_file { warn "edit_file() got called\n" }
>sub close_file { warn "close_file() got called\n" }
>-------------------


Sorry man, its all bullshit.

robic0 03-16-2006 02:56 AM

Re: Using a variable to call a sub-routine...
 
On Wed, 15 Mar 2006 20:35:25 -0500, "Matt Garrish" <matthew.garrish@sympatico.ca> wrote:

>
>"Big Jay" <jbredice@hotmail.com> wrote in message
>news:dP1Sf.1513$tN3.1066@newssvr27.news.prodigy.n et...
>> Hi all,
>>
>> I'm trying to call a sub-routine based on the value of a variable.
>>
>> More specifically, I'm developing a menu system, where each level of the
>> menu has it's own sub-routine, but for a generalized "back" function, I'm
>> trying to call the last menu based on a variable, something like:
>>
>> $lastMenu = apiMenu;
>>
>> &$lastMenu; #I want this to be the same as calling &apiMenu
>>
>> Does this make any sense?
>>

>
>They're called symrefs, and they're to be avoided. It's always better to use
>a hash:
>

Symrefs? Stand for Symbolic References?

Can you please explain, for the beginners, when you use such wild assed terms..
The FLAWWED_FAQ tries I know rescue beginners when it sees such phrases
(that seem to make sence).

However, gone are the days of intellecutal arragoance! If you think your
audience doesen't understand your logic or analogy, its up to *YOU* to
provide the full documentation in your words.

Buddy, who the **** knows what your talking about? Don't invoke
Einstein's theory of realtivity with out explaining yourself!!

I'm gonna hold everyone to this, period !!

-<snip>-


Uri Guttman 03-16-2006 04:31 AM

Re: Using a variable to call a sub-routine...
 
>>>>> "r" == robic0 <robic0> writes:

r> On Wed, 15 Mar 2006 20:15:09 -0600, Tad McClellan <tadmc@augustmail.com> wrote:
>> That is what is known as a "dispatch table".
>>

r> Maybe Tad, you can explain "dispatch table" a phrase used
r> exclusively for NT drivers...
r> Expanding out of Perl Tad?

hmm, did your addlepated brain ever allow the possibility that the term
dispatch table is slightly older than nt? and the concept is even older
than that? you really think that redmond both invented the concept and
the term? i was coding dispatch tables way before uncle bill robbed his
first billion. but you won't understand this anymore than you know why
the sky is blue.

how do you function in the real world with such delusions and
miscomprehensions? but your alter ego rm will soon pop in. ever notice
how the two of you are never seen at the same time?

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org

/usr/ceo 03-16-2006 05:02 AM

Re: Using a variable to call a sub-routine...
 
robic0 wrote:
> On Wed, 15 Mar 2006 20:15:09 -0600, Tad McClellan <tadmc@augustmail.com> wrote:
>
> >Big Jay <jbredice@hotmail.com> wrote:
> >
> >
> >> I'm trying to call a sub-routine based on the value of a variable.

> >
> >
> >That is what is known as a "dispatch table".
> >

> Maybe Tad, you can explain "dispatch table" a phrase used
> exclusively for NT drivers...
> Expanding out of Perl Tad?


Uh, robic0...?? Don't make one of us have to track you down by IP,
come to your cubical at the library, and pull out all your octets one
by one with needle-nosed pliers, ok? Your trolling isn't welcome here.
Maybe try one of the alt groups or something, hmmmm???

/usr/ceo


David K. Wall 03-16-2006 04:26 PM

Re: Using a variable to call a sub-routine...
 
Tad McClellan <tadmc@augustmail.com> wrote:

> Big Jay <jbredice@hotmail.com> wrote:
>
>
>> I'm trying to call a sub-routine based on the value of a
>> variable.

>
>
> That is what is known as a "dispatch table".
>
> You'll need to learn a bit about references to implement
> a dispatch table in Perl.
>
> (the word "subroutine" is not hyphenated.)


I wrote a tentative entry for the FAQ about this a year or two ago,
but never quite got around to submitting it, mainly because I
thought it should be vetted by some with more knowledge and
experience than myself.

Anyway, here it is:



How do I call a subroutine when I have the name of the subroutine?
How do I use a dispatch table?

A dispatch table is just a special name for a hash with code
references. (see L<perlref>) The code references can be from any
source, for example, references to named subroutines in or imported
into a module, references to fully qualified subroutine names, or
anonymous code references.

Here is an example of a dispatch table:

my %dispatch = (
update => \&update,
add => \&add,
other => \&Foo::bar,
error => sub { die "Error: @_" }
default => \&do_something,
);

Put the creation of the dispatch table outside of any subroutine
that uses it so it (the dispatch table) is created at compile time
and not recreated in each call to the subroutine that does the
dispatching.

If we have a desired name in the scalar $input, the associated
subroutine can be called using $dispatch{$input}->().

The code should always handle the case where the input value is not
a key of %dispatch, so we might implement the call like this:

($dispatch{$input} || $dispatch{'default'})->(@arguments);






All times are GMT. The time now is 04:23 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.