Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > calling subroutine , name derived from variable

Reply
Thread Tools

calling subroutine , name derived from variable

 
 
Madhu Ramachandran
Guest
Posts: n/a
 
      01-06-2006
all:

I want to call subroutine, but the name of the subroutine itself is in a
variable. I was able to do this. However, i also want to pass arguments ..
not able to do this.
eg: if $sub has the subroutine name, and $arg has the arg to the subroutine.
How can i call this from my perl main program? i tried eval() and it worked
if i dont have any arguments.

#!/usr/local/bin/perl

$method="aSub";
$aa="\"bla\"";
$mm = "$method" . "($aa)";
print ("mm = $mm\n");
$ret = eval($mm);
print ("ret = $ret\n");
$ret=eval($method);
print ("ret = $ret\n");

sub aSub()
{
my ($arg1) = @_;
print ("inside aSub, arg1=$arg1\n");
return 1;
}
##### Output ######
mm = aSub("bla")
ret =
inside aSub, arg1=
ret = 1
##################

My perl version: This is perl, v5.6.0 built for sun4-solaris

any pointers?

Thanks in advance.

Madhu


 
Reply With Quote
 
 
 
 
Matt Garrish
Guest
Posts: n/a
 
      01-06-2006

"Madhu Ramachandran" <(E-Mail Removed)> wrote in message
news:dpkck1$g8b$(E-Mail Removed)...
> all:
>
> I want to call subroutine, but the name of the subroutine itself is in a
> variable. I was able to do this. However, i also want to pass arguments ..
> not able to do this.


You're looking for symbolic references, but it's not good practice to use
them. For example:

$subRef = 'this_sub';
$arg1 = 'first argument';
$arg2 = 'second argument';

&{$subRef}($arg1, $arg2);

sub this_sub {
print "$_[0] : $_[1]\n";
}


It's better practice to use a hash as you won't break the strictures pragma
that way, which should make your code easier to maintain:

my %subRefs = ( this_sub => \&this_sub );

my $subRef = 'this_sub';

my $arg1 = 'first argument';
my $arg2 = 'second argument';

$subRefs{$subRef}($arg1, $arg2);

sub this_sub {
print "$_[0] : $_[1]\n";
}

Matt


 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      01-06-2006
Madhu Ramachandran wrote:
> I want to call subroutine, but the name of the subroutine itself is in a
> variable. I was able to do this. However, i also want to pass arguments ..


my $method = 'aSub';
my $aa = 'bla';
my $subref = \&$method;
my $ret = $subref->($aa);
print "ret = $ret\n";

sub aSub {
my ($arg1) = @_;
print "inside aSub, arg1=$arg1\n";
return 1;
}

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
robic0
Guest
Posts: n/a
 
      01-06-2006
On Thu, 5 Jan 2006 19:42:03 -0500, "Matt Garrish" <(E-Mail Removed)> wrote:

>
>"Madhu Ramachandran" <(E-Mail Removed)> wrote in message
>news:dpkck1$g8b$(E-Mail Removed)...
>> all:
>>
>> I want to call subroutine, but the name of the subroutine itself is in a
>> variable. I was able to do this. However, i also want to pass arguments ..
>> not able to do this.

>
>You're looking for symbolic references, but it's not good practice to use
>them. For example:
>
>$subRef = 'this_sub';
>$arg1 = 'first argument';
>$arg2 = 'second argument';
>
>&{$subRef}($arg1, $arg2);
>
>sub this_sub {
> print "$_[0] : $_[1]\n";
>}
>
>
>It's better practice to use a hash as you won't break the strictures pragma
>that way, which should make your code easier to maintain:
>
>my %subRefs = ( this_sub => \&this_sub );
>
>my $subRef = 'this_sub';
>
>my $arg1 = 'first argument';
>my $arg2 = 'second argument';
>
>$subRefs{$subRef}($arg1, $arg2);
>
>sub this_sub {
> print "$_[0] : $_[1]\n";
>}
>
>Matt
>


This is bizzar, why would you use a hash of references to subs to call a subroutine?
Any use for that at all? Somebody gonna pass you the name of a subroutine in a packet?

The only "logical" use for an array of subroutines (or function pointers) is the "index",
into it, which means the "name" of the handler is hidden.

-robic0-
actual subroutine reference can be called.

 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      01-06-2006
Gunnar Hjalmarsson wrote:
> Madhu Ramachandran wrote:
>> I want to call subroutine, but the name of the subroutine itself is in
>> a variable. I was able to do this. However, i also want to pass
>> arguments ..

>
> my $method = 'aSub';
> my $aa = 'bla';
> my $subref = \&$method;
> my $ret = $subref->($aa);
> print "ret = $ret\n";
>
> sub aSub {
> my ($arg1) = @_;
> print "inside aSub, arg1=$arg1\n";
> return 1;
> }


The above solution 'works', but I'd better admit that it's actually a
symref, even if it passes strict. Matt's solution was criticized by that
robic0 character, which proves that Matt provided a better answer.

Please disregard my code above.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      01-06-2006
robic0 wrote:
> This is bizzar, why would you use a hash of references to subs to
> call a subroutine? Any use for that at all?


Oh yes, very common scenario in jump tables.

Like in (pseudo-code)
if ($op eq "add) then return $left + $right;
if ($op eq "minus") then return $left - $right;
if ($op eq "div") then return $left / $right;
...
return "Unknown op $op";

This can be written much better as

if (exists($op, %optable)) {
$optable->$op ($left, $right);
} else {
throw_error "Unknown op $op";
}

jue


 
Reply With Quote
 
robic0
Guest
Posts: n/a
 
      01-06-2006
On Fri, 06 Jan 2006 04:16:28 GMT, "Jürgen Exner" <(E-Mail Removed)> wrote:

>robic0 wrote:
>> This is bizzar, why would you use a hash of references to subs to
>> call a subroutine? Any use for that at all?

>
>Oh yes, very common scenario in jump tables.
>
>Like in (pseudo-code)
> if ($op eq "add) then return $left + $right;
> if ($op eq "minus") then return $left - $right;
> if ($op eq "div") then return $left / $right;
> ...
> return "Unknown op $op";
>
>This can be written much better as
>
> if (exists($op, %optable)) {
> $optable->$op ($left, $right);
> } else {
> throw_error "Unknown op $op";
> }
>
>jue
>


Whats a "jump" table? Whats pesuedo-code?
$i = 0
$i = 1-$i
How far does extrapolation go (down)?
Is there logic I'm unaware of?
Can I be fooled?
Do I have time for un-paid thought?
Have I done almose everything?
Can a resume save me?
-robic0-
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      01-06-2006
Madhu Ramachandran <(E-Mail Removed)> wrote:

> I want to call subroutine, but the name of the subroutine itself is in a
> variable.



That is called a "symbolic reference".

Use a real reference instead:

perldoc perlreftut

perldoc perlref



> sub aSub()

^^
^^

Why did you put that there?

Do you know what it does?

Is what it does what you want to have done?


> {
> my ($arg1) = @_;



I think not.


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      01-06-2006
Tad McClellan wrote:
> Madhu Ramachandran wrote:
>>{
>> my ($arg1) = @_;

>
> I think not.


Why not?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
robic0
Guest
Posts: n/a
 
      01-06-2006
On Thu, 5 Jan 2006 22:32:33 -0600, Tad McClellan <(E-Mail Removed)> wrote:

>Madhu Ramachandran <(E-Mail Removed)> wrote:
>
>> I want to call subroutine, but the name of the subroutine itself is in a
>> variable.

>
>
>That is called a "symbolic reference".
>

Why in hell would you use it in the context of a hash of "subroutines" ?
Get off your micro-analysis and look at it from a design point of reference.
Don't ever, ever take any class I teach...

>Use a real reference instead:
>
> perldoc perlreftut
>
> perldoc perlref
>
>
>
>> sub aSub()

> ^^
> ^^
>
>Why did you put that there?
>
>Do you know what it does?
>
>Is what it does what you want to have done?
>
>
>> {
>> my ($arg1) = @_;

>
>
>I think not.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Variable variable name" or "variable lvalue" mfglinux Python 11 09-12-2007 03:08 AM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
use one subroutine's variable value in another subroutine inside a module. king Perl Misc 5 04-29-2007 06:39 AM
adding a variable name to a hash to name is part of the variable name Bobby Chamness Perl 2 04-22-2007 09:54 PM



Advertisments