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

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

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