Hey Paul,

That's a clever way of achieving the task.

Thanks for that.

With Best regards,
R. Kamal Raj Guptha.
Ph: 91-80-28411990 Ext:1296
Mobile - 9880351218


>-----Original Message-----
>From: Paul Johnson [mailto:[EMAIL PROTECTED]
>Sent: Friday, November 26, 2004 5:32 PM
>To: R. Kamal Raj Guptha (WT01 - TELECOM SOLUTIONS)
>Cc: [EMAIL PROTECTED]
>Subject: Re: Calling functions using variable (I mean symbolic refs)
>
>
>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
>


Confidentiality Notice

The information contained in this electronic message and any attachments to 
this message are intended
for the exclusive use of the addressee(s) and may contain confidential or 
privileged information. If
you are not the intended recipient, please notify the sender at Wipro or [EMAIL 
PROTECTED] immediately
and destroy all copies of this message and any attachments.

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