Re: [Boston.pm] return value of string eval()?

Uri Guttman <uri-X/[email protected]> Thu, 25 Apr 2019 12:48:46 -0400
Newsgroups gmane.comp.lang.perl.perl-mongers.boston
Organization Perl Hunter
Message-ID <[email protected]>
On 4/25/19 12:18 PM, Greg London wrote:
>
> I thought the return value of eval(<my string of perl code>) was
> defined if eval'ed oK
> undef if failed to eval OK.
>
> I have some code that runs without any issue when directly executed.
> But errors out when I have it as part of a string eval().
>
> My script is doing that eval( my string ) version
> and is bombing out because the return value is not defined.
>
> Far as I can tell the code should compile and execute fine.
>
> Am I misunderstanding teh return value of eval()?
from perldoc -f eval:

If there is a syntax error or runtime error, or a "die" statement
             is executed, "eval" returns "undef" in scalar context, or 
an empty
             list in list context, and $@ is set to the error message.
If there was no error,
             $@ is set to the empty string.

so you need to check $@ to see if there really was an error. eval will 
return the last value of the expression (like a sub with without a return).


it is interesting that you bring this up. i have a talk scheduled for 
yapc called 'perl begat perl' which is all about generating perl inside 
perl and calling eval on the code. if it works out, i will want to do 
the talk at boston.pm before yapc. the schedule looks like it should 
work. i do have to get working on the slides already!

uri

_______________________________________________
Boston-pm mailing list
[email protected]
https://mail.pm.org/mailman/listinfo/boston-pm