[EMAIL PROTECTED] (TapasranjanMohapatra) writes:

> Now I use this module in another script. I want to call the sub routines, as 
> suggested by the argument passed to the script.
> i.e. 
> my_script q  should call the sub routine zzzq,
> my_script e should call the sub routine zzze,
> ...
> 
> when there are many sub routines, 
> if i get the name of subroutine as
> 
> $name = "zzz".$argument_received;
> 
> can I call the sub routine as
> 
> abc::$name;
> 
> This does not work.
> Any suggestion, to get this done?


I'm guessing that your primary mission objective is to parse one of
several commands at run-time ... 

Here is one of my favorite idioms for that is to use a simple hash of
coderefs as a dispatch table.  

Dealing with parameters is left as an exercise to the reader.

###################################################
# in abc.pm:
###################################################
use strict;
use warnings; 

package abc; 

sub hello {
    print "Hello world!\n";
    return 1; 
}

sub goodbye {
    print "I say hello - you say goodbye\n";
    return 0; 
}

sub cookie {
    print "`C' is for cookie, that's good enough for me\n";
    return 1; 
}

1;

##################################################
# test.pl
##################################################

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

use lib '/tmp';  # or wherever abc lives.
use abc; 

print "Valid commands are `hello', `goodbye', and `cookie'\n"; 
my $continue = 1; 
while($continue) { 
    print "Enter a command: "; chomp(my $command = <STDIN>); 
    my %dispatch = ( hello =>   \&abc::hello,
                     goodbye => \&abc::goodbye,
                     cookie =>  \&abc::cookie ) ;
    
    if (exists($dispatch{$command})) {
        $continue = &{$dispatch{$command}};
    } else {
        warn "Unimplemented command: $command\n";
    }
}



-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
        Lawrence Statton - [EMAIL PROTECTED] s/aba/c/g
Computer  software  consists of  only  two  components: ones  and
zeros, in roughly equal proportions.   All that is required is to
sort them into the correct order.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to