Here is a small script which loads some subs and then depending on
what you enter, then executes the sub inputted:

#!perl -w

my %MyHash = qw(sub0 1 sub1 1 sub2 1  sub3 1 sub4 1 sub5 1
                sub6 1 sub7 1 sub10 1 sub11 1 );

foreach my $MyKeys (sort keys %MyHash) {
   printf "%-s\n", $MyKeys;
 }
 
while ( 1 ) {
  printf "Please enter Sub to execute: ";
  chomp(my $MyResp = <STDIN>);
  last if ( $MyResp =~ /^(qu|ex)$/i );
  my $MyKey = sprintf "%-s", $MyResp;
  if ( ! defined $MyHash{$MyKey} ) {
     printf "No sub exists for %-s\n", $MyKey;
     next;
   }
  &$MyKey;
 }
sub sub0 {
   printf "%-s\n", (caller(0))[3];
 }

sub sub1 {
   printf "%-s\n", (caller(0))[3];
 }

sub sub2 {
   printf "%-s\n", (caller(0))[3];
 }

sub sub3 {
   printf "%-s\n", (caller(0))[3];
 }

sub sub4 {
   printf "%-s\n", (caller(0))[3];
 }

sub sub5 {
   printf "%-s\n", (caller(0))[3];
 }

sub sub6 {
   printf "%-s\n", (caller(0))[3];
 }

sub sub7 {
   printf "%-s\n", (caller(0))[3];
 }

sub sub10 {
   printf "%-s\n", (caller(0))[3];
 }

sub sub11{
   printf "%-s\n", (caller(0))[3];
 }
 

        Hopefully helpful.

Wags ;)
-----Original Message-----
From: nkuipers [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, September 24, 2002 11:11
To: Zielfelder, Robert
Cc: [EMAIL PROTECTED]
Subject: RE: Using a variable name to invoke a subroutine


>foreach $sub (@list_of_subs) {
>       &{$sub};  ##-- this is the part I am stuck on.  This doesn't work
>}

I don't know the answer to your question; I'm also interested in what others

have to say.  However, I have to wonder if Perl is looking at &{$sub} and 
interpretting it as a command to execute an anonymous sub that returns the 
value of $sub, in other words, Perl is perhaps not Doing What You Mean.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


**********************************************************
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.
****************************************************************


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to