Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to call a method with arguments via eval ? 2

Status
Not open for further replies.

iamberb

Programmer
Jul 14, 2005
15
DE
Hi,

I have problems with eval when I want to call a procedure inside eval which passes a argument.
This line fails when i pass $firstname. If I call without argument it's fine.
my $code = eval "sub {get_lastname($firstname);}";
Error:
Undefined subroutine &main:: called at V:\BERB_view\FINANCE_SUPPORT\Tools\Ration
al\ClearCase\scripts\bin\eval_test.pl line 7.

Please don't reply about the sense of the sample as it's just for evaluation reason to find out what I can do with eval.

Full Sample:
my $code = eval "sub { print }";
&$code;
$lastname=get_lastname("Robert");
print ("LastName: $lastname\n");
$firstname="Peter";
my $code = eval "sub {get_lastname($firstname);}";
$lastname=&$code;
print ("LastName: $lastname\n");

sub get_lastname ()
{
$firstname=$_[0];
print ("FirstName:$firstname\n");
return "Armadillo" if ($firstname eq "Robert");
return "Walls" if ($firstname eq "Peter");
}
 
You're misunderstanding what eval does. eval returns a result, however what you are after is a code-ref:

Code:
my $coderef = sub { print };
&$coderef;          # this executes the code
eval { &$coderef }; # this executes the code safely

See perlref [1] for further details.

[1]
Barbie
Leader of Birmingham Perl Mongers
 
Hi Barbie,

But can a dynamically call get_lastname() with some arguments.
e.g. eval "sub {get_lastname('Robert');}"; or
$firstname="Robert";
eval "sub {get_lastname($firstname);}";
What I am doing wrong ?
It works fine to call get_lastname as long i don't pass an argument.

Thanks!
brg Robert
 
Can you explain what you mean by `dynamically call' a subroutine? I'm can't tell what exactly you're trying to do.

To expand on Barbie's example to pass parameters to subroutine references:
Code:
my $code = sub { print shift };
$code->( 'robert' );
 
Take a look at the perldoc page for eval [1]. There is a difference between an expression and a block form of eval. You are using the expression form, but really want the block form.

[1]
Code:
my $firstname='Robert';
eval { sub {get_lastname($firstname);} };

Barbie
Leader of Birmingham Perl Mongers
 
Hi,

What I want is to execute some code dynamically.
Meaning I want to have some statements inside a string.
e.g. $code="check_Name('Robert') && check_name('Hans')";
Than I want to execute the content of $code like i would execute the check_Name commands directly.
Difference is only that in my case they are contained in the variable $code.

Lets assume you read a string: <Robert> && <Hans>
This i replace by: check_name("Robert") && check_name("Hans").
But how to compare was dynamically read in.(could be any && or || combination.

I am not sure if I can explain good enough what I want to reach.

I was thinking to use this for argument checks.
Arguments are allowed to exist in a certain combination.
So you could pass:
(<arg1> || <arg2> && (<arg3>))
this you convert to: (check_arg(arg1) || check_arg(arg2) && (check_arg(arg3)))
Then execute dynamically and check the return value.

Thanks for helping so far,
robert


brg robert
 
Hopefully the following will clarify what you want:

Code:
#!/usr/bin/perl -w
use strict;

my $string = '(<arg1> || <arg2> && (<arg3>))';
$string =~ s/<(.*?)>/check_arg($1)/g;   # global string replace

print "$string\n"; # prints '(check_arg(arg1) || check_arg(arg2) && (check_arg(arg3)))'

$string = '(<arg1> || <arg2> && (<arg3>))';
$string =~ s/<(.*?)>/check_arg($1)/ge;  # global string evaluation replace

print "$string\n"; # prints '(1 || 2 && 3))'

my $result = eval "$string";
print "$result\n"; # prints '1' the result of evaluting '(1 || 2 && 3))'

sub check_arg {
    my $arg = shift;
    $arg =~ tr/a-z//d;
    return $arg;
}

Barbie
Leader of Birmingham Perl Mongers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top