On Fri, Nov 26, 2004 at 12:15:12AM +0530, [EMAIL PROTECTED] wrote:
> Hi all,
>
> May be this is a OLD topic of discussion.
>
> I have a Perl script with lot of functions. This Perl script accepts two
> command line options (string values) where the concatenation of these
> two strings will give me the function name.
>
> All the functions in this Perl script are like this.
>
> For eg.,
>
> #!/usr/bin/perl
>
> use strict;
>
> use Getopt::Long;
>
> my $rc = GetOptions( "class|c=s"
> ,"method|m=s"
> ,"help|h|?"
> ,"trace:i"
> );
> {
> no strict 'refs';
> my $functionname = $options{class}."_".$options{method};
>
> &$functionname;
> }
>
> sub Class1_Method1 {
>
> print "Class 1 Method1\n";
>
> }
>
> sub Class1_Method2 {
>
> print "Class 1 Method2\n";
>
> }
>
> sub Class2_Method1 {
>
> print "Class 2 Method1\n";
>
> }
>
> My question now is in many docs that I have read they say that it is
> always better to avoid symbolic references. Is there any better way of
> implementing the above mentioned scenario, If so do suggest and explain.
You talk about classes and methods, yet I see only plain subroutines.
If you did have classes and methods you could do something like:
my $o = $class->new;
$o->$method;
With plain subs you could do something like:
my $subs =
{
Class1_Method1 => sub { print "Class 1 Method1\n" },
Class1_Method2 => sub { print "Class 1 Method2\n" },
Class2_Method1 => sub { print "Class 2 Method1\n" },
};
my $sub = "$options{class}_$options{method}";
die "$sub does not exist" unless exists $subs->{$sub};
$subs->{$sub}->();
--
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>