TapasranjanMohapatra wrote:
Hi All,

Hello,

I have a querry if the following can be possible.

Suppose I have many sub routines in a module abc.pm

package abc;

Your package should use strict and warnings :)

sub zzzq
{
}

sub zzze
{
}
sub zzzr
{
}

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.

You are loooking for soft references:

 my $name = $ARG[0] || 'zzz';

no strict 'refs';
$name->($argument_received);
use strict 'refs';


What would be better still is to use hard refs like this:

my %func = (
 q => \&abc::zzzq,
 r => \&abc::zzzr,
);

my $name = $ARGV[0] || 'q';

die 'Invalid Arguament' unless exists $func{$name};
$func{$name}->(@your_args_to_the_function);

That way you can leave use strict; alone and check for valid arguments easily :)

HTH :)

Lee.M - JupiterHost.Net

--
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