Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

require in .pm and .pl = blow up?

by Anonymous Monk
on Aug 14, 2000 at 23:15 UTC ( [id://27809]=perlquestion: print w/replies, xml ) Need Help??

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

why does a double require break the script? the output: main.pl = foo.pm = I AM BAR. (they should both print "I AM BAR.") main.pl: use foo; require "vars.pl"; print "main.pl = $bar\n"; foo.pm: package foo; require "vars.pl"; print "foo.pm = $bar\n"; 1; vars.pl: $bar="I AM BAR.";

Replies are listed 'Best First'.
RE: require in .pm and .pl = blow up?
by tye (Sage) on Aug 14, 2000 at 23:35 UTC

    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")
      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")
Re: require in .pm and .pl = blow up?
by turnstep (Parson) on Aug 14, 2000 at 23:19 UTC

    Our esteemed colleague AM meant to say:

    ## the output: main.pl = foo.pm = I AM BAR. (they should both print "I AM BAR.") main.pl: use foo; require "vars.pl"; print "main.pl = $bar\n"; foo.pm: package foo; require "vars.pl"; print "foo.pm = $bar\n"; 1; vars.pl: $bar="I AM BAR.";
Re: require in .pm and .pl = blow up?
by le (Friar) on Aug 14, 2000 at 23:27 UTC
    This is just a wild guess, but maybe there's a missing "1;" and the end of vars.pl.
      A decent guess, but not the case.

      Perl requires that the last statement in a module be true... a variable assignment returns a true value, so that'll work.

      Generally people put 1; as the last line in module just because there's almost no way it can ever evaluate to anything but true, whereas lots of other seemingly straightforward statements actually might. See perlmod for details.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://27809]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-24 16:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found