Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Using a variable to call a sub-routine...

Reply
Thread Tools

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

 
 
Big Jay
Guest
Posts: n/a
 
      03-15-2006
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!


 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      03-16-2006
"Big Jay" <(E-Mail Removed)> wrote in
news:dP1Sf.1513$(E-Mail Removed) 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 <(E-Mail Removed)>
(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

 
Reply With Quote
 
 
 
 
Big Jay
Guest
Posts: n/a
 
      03-16-2006
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" <(E-Mail Removed)> wrote in message
news:dP1Sf.1513$(E-Mail Removed) 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!
>



 
Reply With Quote
 
Matt Garrish
Guest
Posts: n/a
 
      03-16-2006

"Big Jay" <(E-Mail Removed)> wrote in message
news:dP1Sf.1513$(E-Mail Removed) 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


 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      03-16-2006
Big Jay <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
robic0
Guest
Posts: n/a
 
      03-16-2006
On Wed, 15 Mar 2006 20:15:09 -0600, Tad McClellan <(E-Mail Removed)> wrote:

>Big Jay <(E-Mail Removed)> 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.
 
Reply With Quote
 
robic0
Guest
Posts: n/a
 
      03-16-2006
On Wed, 15 Mar 2006 20:35:25 -0500, "Matt Garrish" <(E-Mail Removed)> wrote:

>
>"Big Jay" <(E-Mail Removed)> wrote in message
>news:dP1Sf.1513$(E-Mail Removed) 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>-

 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      03-16-2006
>>>>> "r" == robic0 <robic0> writes:

r> On Wed, 15 Mar 2006 20:15:09 -0600, Tad McClellan <(E-Mail Removed)> 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 ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
/usr/ceo
Guest
Posts: n/a
 
      03-16-2006
robic0 wrote:
> On Wed, 15 Mar 2006 20:15:09 -0600, Tad McClellan <(E-Mail Removed)> wrote:
>
> >Big Jay <(E-Mail Removed)> 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

 
Reply With Quote
 
David K. Wall
Guest
Posts: n/a
 
      03-16-2006
Tad McClellan <(E-Mail Removed)> wrote:

> Big Jay <(E-Mail Removed)> 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);




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using string variable to call a method Bryan Richardson Ruby 6 01-08-2008 11:38 PM
"Variable variable name" or "variable lvalue" mfglinux Python 11 09-12-2007 03:08 AM
how to call a function using variable Amaninder Perl Misc 5 06-16-2006 02:56 AM
Using variable or using direct method call - Java 4 06-01-2005 04:30 PM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM



Advertisments