Rodney_Hampton has asked for the wisdom of the Perl Monks concerning the following question:

Ok, code snippet 1 works fine and $stuff and $stuff_error
contain the appropriate output from
Util::Test_Util::test_expand. Problem is I'd like to
do some interpolation for parts of this eval
expression. I haven't added any interpolated variables
to the eval string yet. I'd just like to
know what I'm doing wrong in code snippet 2. I can't
seem to quote the basic eval correctly.

===========snippet 1 works ========

eval{ use Util::Test_Util qw/ test_expand /; ($$stuff,$$stuff_error)=Util::Test_Util::test_expand(\@link_names); if ($stuff){return(1);}else{return(0);} };
============end snippet 1 ========

============snippet 2 fails ========

eval{" use Util::Test_Util qw/ test_expand /; (\$\$stuff,\$\$stuff_error)=Util::Test_Util::test_expand(\\\@link_name +s); if (\$stuff){return(1);}else{return(0);} "};
============end snippet 2 ========

Rodney A. Hampton

Replies are listed 'Best First'.
Re: eval quoting problems
by nothingmuch (Priest) on Oct 22, 2002 at 19:03 UTC
    You are simply returning a string from the eval. Ommit the block curlies: eval {""} to eval ""

    In terms of maintainability and ease (and saving the optimiser some work), I think that you should nest an eval in an eval if possible, to minimise the interpolating run-time compilation, and get around mistakes. Also, where not needed you can add single quotes, and concatenate them with the variables (outside quoes) like this:
    eval 'use Util::Test_util' # goes on . $var_that_needs_interpolation . 'do_stuff($var)'; # var is not interpolated
    Update: I forgot to say that by nesting i mean use an eval EXPR within an eval BLOCK.

    -nuffin
    zz zZ Z Z #!perl