in reply to eval and variable scope

I don't have an answer, but i do have some interesting test results:
# version 5.006 unable to open file file3: unable to open file file2: unable to open file file1: No such file or directory # version 5.006001 unable to open file file3: unable to open file file2: unable to open file file1: No such file or directory # version 5.007002 unable to open file file3: No such file or directory unable to open file file2: No such file or directory unable to open file file1: No such file or directory
Looks like the only one that didn't 'squash' $! was 5.7.2.

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
F--F--F--F--F--F--F--F--
(the triplet paradiddle)

Replies are listed 'Best First'.
Re: (jeffa) Re: eval and variable scope
by seattlejohn (Deacon) on Dec 17, 2001 at 07:16 UTC
    Thanks, jeffa!

    Since this doesn't seem to behave consistently between different versions of Perl, I'll probably have to adopt a different approach in my code. But at least now I know I'm not going insane ;-)

      I'm stumped as well.... If you're looking for a different approach, you might try replacing eval '...' with do{...}

      I know this is a trimmed down example, but this seems to work for me:

      ... or print do {qq{unable to open file $items[2]: $!\n}};

      -Blake

        Thanks for the suggestion. Unfortunately this approach won't work for the particular situation I'm dealing with, in which the string "templates", as it were, have to be defined before the interpolation can take place (which is why I can't just use qq{} in the first place). Basically what I was experimenting with was having a set library of phrases whose contents would be interpolated in the appropriate context at a later time. Roughly:
        %messages = ( file_open_failed => 'unable to open $_[0]: $!', some_other_error => 'had a problem doing $_[0]', ); ... stuff happens ... open (INPUT, $filename) or die interpolate('file_open_failed', $file +name);
        (This behavior is actually encapsulated in an object, etc.; again, I've just stripped down for simplicity's sake.) Anyway, I got this all working fine initially using closures, and just started trying the eval-string approach as an alternative (TMTOWTDI...) In any case, I appreciate the reminder about do's existence, since it's not something I use with any regularity!