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

When I eval code- is it best to do it like this:
Somefile:
{ my $thing_file_dot_inc_needs = 'poopoo'; # Do some more of those. do file.inc; }
Or is there some better way?

Replies are listed 'Best First'.
Re: Some Advice On Eval()-ing code.
by tall_man (Parson) on Feb 13, 2003 at 05:16 UTC
    There are several reasons not to use "do" that way. First, from the perldoc man page:

    ...code evaluated with do FILENAME doesn't see lexicals in the enclosing scope like eval STRING does.

    In other words, your "$thing_file_dot_inc_needs" variable won't work. Here's another flaw:

    ...it does reparse the file every time you call it...

    It would be better to make the "file.inc" into a module and bring it in with a "use" or "require". Then you are protected from double-inclusion. Lastly, "do" is a heavily-overloaded keyword, also used for loops and subroutines, so it makes the program less clear, IMHO.