calling subroutine , name derived from variable

  • Thread starter Madhu Ramachandran
  • Start date
T

Theo van den Heuvel

Don't ever, ever take any class I teach...

It took me a while to recover from this. I think a gentle hint
is called for. You shouldn't teach. You shouldn't teach, because
you suffer from a disposition for which, if I recall correctly,
the official medical term is stupidity. Not a mild form of it,
but the rarest variety of unmitigated, terminal stupidity.
You have proven this time and again. As a recent example,
I remember your rants on parsing when you tried to tackle
XML with regexes. So I urge you: do consider your pupils.
They might learn something from you. You should definitely
not teach.
 
C

ced

Try:

#!/usr/local/bin/perl

use strict;
use warnings;

my $method="aSub";
my $aa= 'bla';

no strict;
^^^^^^^^^^^

I've forgotten where I saw the doc ref. but the &$method
call is ignored by 'strict' so you don't even need 'no strict'.
my $ret = eval(&$method ($aa));
...

hth,
 
S

Samwyse

Tad said:
You are not fooling anyone.

If you truly were thankful, then you would follow the quoting
conventions that folks expect here (and nearly everywhere else too).

I personally consider top-posting forgivable for a simple thank-you,
since the preceding conversational context isn't needed to understand
what's going on.

Full-quoting, OTOH, is definitely evil.
 
E

Eric J. Roode

(e-mail address removed) wrote in
Try:

#!/usr/local/bin/perl

use strict;
use warnings;

my $method="aSub";
my $aa= 'bla';

no strict;
my $ret = eval(&$method ($aa));

use strict;
print "ret = ", $ret "\n";

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


Did *you* try it?

Please, don't post code unless you really know that it works.
Test first.

--
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 
B

Brian McCauley

Matt said:
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:

Beware the knee-jerk anti-symref reaction.

There are a lot of things wrong with using symbolic references, but
many of perfectly valid arguments against using symbolic references
simply don't apply in the context of symbolic code references. Indeed
Perl's own version of OO is effectively implemented using symbolic code
referecence.

It is often good practice to place all the subroutines you want to use
into a package that is used for nothing else. Doing so negates the two
major problems with using symrefs (security and conceptual code/data
separation)..

my $subRef = 'this_sub';

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

# Use a symref one way...
(\&{"My::Dispach::package::$subRef"})->($arg1, $arg2);

# ... or another
{
no strict 'refs';
"My::Dispach::package::$subRef"->($arg1, $arg2);
}

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

Matt Garrish

Brian McCauley said:
Matt Garrish wrote:


It is often good practice to place all the subroutines you want to use
into a package that is used for nothing else. Doing so negates the two
major problems with using symrefs (security and conceptual code/data
separation)..


# Use a symref one way...
(\&{"My::Dispach::package::$subRef"})->($arg1, $arg2);

# ... or another
{
no strict 'refs';
"My::Dispach::package::$subRef"->($arg1, $arg2);
}

The latter has actually become my preferred choice for calling functions
based on a parameter passed to a script, but I expected that advocating it
would just lead to an endless discussion of the evils of symrefs. Nice to
see I'm not the only one...

Matt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top