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")In reply to RE: require in .pm and .pl = blow up?
by tye
in thread require in .pm and .pl = blow up?
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |