in reply to Problem with require and default package

require only loads a file once. If you want to load a file multiple time, have a look at do.

  • Comment on Re: Problem with require and default package

Replies are listed 'Best First'.
Re^2: Problem with require and default package
by narainhere (Monk) on Dec 21, 2007 at 10:17 UTC
    So does it imply that 'require' just Pulls in the contents whereas 'do' executes them??

    The world is so big for any individual to conquer

      No, they both execute the code. perlfunc discusses the difference and generally encourages require over do. Another possibility is to remove the file's entry from %INC so that perl loads the file again, for example:
      require 'gash2.pl'; $var = 37; # do some stuff delete $INC{'gash2.pl'}; require 'gash2.pl';
        They both execute the code. But the difference that Corion alerted to is that require includes and compiles the code only once.

        So, in the case of the OP, $var was first initialized in env.pl and then given the new value 'default' inside the loop. When env.pl was required the second time nothing happened because it was already loaded, so $var was not affected.
        If, instead of a require, a do was issued, env.pl would have been compiled again and $var would be reassigned.

        require will look for an entry of the required file on %INC. If found, does nothing.
        do ignores %INC and loads the file. Then creates/updates the entry on %INC.