Ask a Question related to PERL Miscellaneous, Design and Development.
-
A.J. #1
return() inside eval
This works as needed:
#!/usr/local/bin/perl
use strict;
use Exception::Class( 'Exception' );
sub test() {
eval {
return( param1 => 'first',
param2 => 'second' );
};
#print "here\n";
}
my %ret = &test();
print "param1:".$ret{param1}."\n";
print "param2:".$ret{param2}."\n";
param1:first>./test.pl
param2:second
This doesn't. Why doesn't return work
properly in this case. Why is the print
clause executed? perl5.8.0
#!/usr/local/bin/perl
use strict;
use Exception::Class( 'Exception' );
sub test() {
eval {
return( param1 => 'first',
param2 => 'second' );
};
print "here\n";
}
my %ret = &test();
print "param1:".$ret{param1}."\n";
print "param2:".$ret{param2}."\n";
here>./test.pl
param1:
param2:
A.J. Guest
-
#39982 [NEW]: when used inside a function, proc_open generated pipes are closed upon return
From: federico at galassi dot net Operating system: gentoo linux PHP version: 5.2.0 PHP Bug Type: Program Execution Bug... -
Can't use return key to edit text inside a text box
When I initially type a text in a text box, I can use the return key to create a space between paragraphs. Once I leave the text box I can't go back... -
Dynamic temp. datagrid col.gen. -Session access inside a class inside a UserCtrl
Hello Dear Professionals: Based on this document:... -
Eval()
I know i could use EVAL to evaluate VARs but what about if i want to evaluate a Var that have a funtion onit? like A="math.Cos(65)" B=Eval(A);... -
eval'
I am retrieving the following string from the database. " and upper(gsa_template_data_header) >= '".$start_selection."' and... -
Stefan #2
Re: return() inside eval
Using return within your eval returns from the eval block, not the
surrounding subroutine. The reason the first version works is that the
eval is the last statement, and therefore it's value is used as the
return value. In the first version you are returning eval which in turn
returns your hash. In the second version you are running eval, ignoring
the result and returning print.
Stefan
A.J. wrote:> This works as needed:
>
> #!/usr/local/bin/perl
> use strict;
> use Exception::Class( 'Exception' );
>
> sub test() {
> eval {
> return( param1 => 'first',
> param2 => 'second' );
> };
> #print "here\n";
> }
>
> my %ret = &test();
> print "param1:".$ret{param1}."\n";
> print "param2:".$ret{param2}."\n";
>
>>>>./test.pl
> param1:first
> param2:second
>
> This doesn't. Why doesn't return work
> properly in this case. Why is the print
> clause executed? perl5.8.0
> #!/usr/local/bin/perl
> use strict;
> use Exception::Class( 'Exception' );
>
> sub test() {
> eval {
> return( param1 => 'first',
> param2 => 'second' );
> };
> print "here\n";
> }
>
> my %ret = &test();
> print "param1:".$ret{param1}."\n";
> print "param2:".$ret{param2}."\n";
>
>>>>./test.pl
> here
> param1:
> param2:
>
>
>Stefan Guest
-
A.J. #3
Re: return() inside eval
The reason I want to use eval is to catch
possible exceptions inside various subroutines.
I'm using Exception::Class. Is there a better
way of doing this?
sub test() {
eval {
do something..
return( param1 => 'first',
param2 => 'second' );
};
if( UNIVERSAL::isa( $@, 'Exception' ) ) {
return( param1 => 'Catched exception:'.$@->message,
param2 => '' );
}
}
A.J. Guest
-
Stefan #4
Re: return() inside eval
A.J. wrote:
Sure, just make sure you put your result in a variable before returning> The reason I want to use eval is to catch
> possible exceptions inside various subroutines.
> I'm using Exception::Class. Is there a better
> way of doing this?
it. For example:
sub test() {
my %result;
eval {
do something...
%result = (
param1 => 'first',...
);
}
if ($@) {
...
}
else {
return %result;
}
}
Stefan Guest



Reply With Quote

