in reply to (jeffa) Re: eval and variable scope
in thread eval and variable scope

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 ;-)

  • Comment on Re: (jeffa) Re: eval and variable scope

Replies are listed 'Best First'.
Re: Re: (jeffa) Re: eval and variable scope
by blakem (Monsignor) on Dec 17, 2001 at 07:22 UTC
    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!