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

Hi, I've read the perl monks tutorial on including files and I think I understood most of it. But I would still like to include a file, so I'm going to ask anyway ;-) The file I'd like to include is sort of like an argument to the script - sort of like a config file but more complex and varies when I call the script. There are a whole bunch of them and I'd I like to be able to say:
$ my-script.plx <file> <more arguments>
(Or more likely, in the shell:
$ for i in *some-pattern* ; do my-script.plx $i <further args> ; done)
Usually when I do this the file is very simple, but in this case it has structure, so I coded that in perl like:
%hash1 = (fruits => ["apple", "pear"], things => ["table", "chair"] ); %hash2 = (fruits => ["pear", "banana"] countries => ["Turkey"] );
Using pure text and parsing it with split, ... instead of Perl code seems like quite a hassle I'd like to avoid. Is there any way to do this in Perl? The one thing I didn't understand is what "eval" does, and I couldn't get the syntax right for this one. Does anyone have a hint for me? I'd also welcome ideas on how to re-design this, if it's the case that I'm being blind as to a much easier way ;-)

Replies are listed 'Best First'.
Re: Including files (again)
by BrowserUk (Patriarch) on Jan 27, 2009 at 10:43 UTC
      You probably want to check the return value of 'do'. Unlike require (which could be used here instead of 'do' as well), 'do' won't die if there's a compilation error in $include - it will even continue if it cannot find or read $include.
Re: Including files (again)
by cdarke (Prior) on Jan 27, 2009 at 14:16 UTC
    $ for i in *some-pattern* ; do my-script.plx $i <further args> ; done)
    In sh/ksh/bash this does not "include" another file - it runs a child process. In those shells you need the . (dot) command, or 'source' in bash to "include" (read in then execute) another file.

    Or is it that you just want to run another script? In which case, use system.