in reply to namespaces and variable initilization with require 'file'

require is for modules (files with a package directive).
do is for modulesscripts (files without a package directive).

It might even fix your problem without further change, but I'm not sure.

Replies are listed 'Best First'.
Re^2: namespaces and variable initilization with require 'file'
by JadeNB (Chaplain) on Oct 01, 2008 at 22:04 UTC
    Did you really mean to use the word 'modules' for both kinds of files? I guess in any case that you didn't mean that require can only be used on files with a package directive, because that's certainly not true:
    $ echo 'print "Included a.pm\n";' > a.pm $ perl -e 'require a' # Included a.pm
    works just fine for me. I understood a big difference (though by no means the only one) to be between whether you want to allow the file to be read multiple times, in the sense that
    $ perl -e 'require a; require a' # Included a.pm $ perl -e 'do "a.pm"; do "a.pm"' # Included a.pm\ # Included a.pm
    do different things.

      The repeated use of "modules" was a copy and paste error.

      And right, I wasn't talking about the abilities of the respective commands, but their purpose. do executes a file. So does require. The difference is that require will only execute a file once per run, so it only makes sense to use require for files that load a module.