in reply to require in .pm and .pl = blow up?

require only parses a file if it hasn't already been required before. The first time vars.pl is required occurs in foo.pm in the package "foo" and so $bar= "I AM BAR." actually sets $foo::bar= "I AM BAR.". The second time vars.pl is required, Perl says, "Yes, you require that file and I've already loaded it so I don't need to do anything here". So $main::bar is never set.

If you want to set a variable in the caller's package every time your file is required, then I suggest you make the file a real module, like:

package MyVars; use base qw(Exporter); use vars qw(@EXPORT_OK $bar); @EXPORT_OK= qw($bar); $bar="I AM BAR."; 1;

Then you use it via use MyVars qw($bar);.

You can also include files via "do" to have them parsed every time, but I strongly frown on that.

        - tye (but my friends call me "Tye")

Replies are listed 'Best First'.
RE: RE: require in .pm and .pl = blow up?
by Anonymous Monk on Aug 15, 2000 at 01:26 UTC
    OK. I'm getting closer to getting this. Thanks for the help.. The problem is that I'm working on a system with severely horrible code, and I can't change existing scripts.. So basically I need a way to 'secretly' require files inside foo.pm without letting main:: know that I required them.. (so existing scripts that depend on the .pl's will not break) Know what I mean? 'do' will force the .pl to be reparsed, but it also remembers what it read, like 'require' do I have to do something yucky like dumping the file into a scalar and eval'ing it?

      In that particular situation, I'd probably resort to the rather yucky:

      require "vars.pl"; delete $INC{"vars.pl"};

      As slick as that seems, I still think it is pretty yucky. I'd not give up on the political side of getting access to change existing horrible things.

              - tye (but my friends call me "Tye")