ptum has asked for the wisdom of the Perl Monks concerning the following question:
I originally learned what Perl I know back in the 90's, and tended then (and still do, sometimes) to put common subroutines into a 'standard.pl' library which I would 'require' where needed. I know that most people bundle such routines into a module these days, and I understand some of the reasons for this. I was burned today and wondered if someone could explain to me (using small words) just why.
Suppose I have the following modules:
package A; use B; require 'standard.pl'; sub new { # [snip] bless ($self, $class); do_something(); # subroutine in standard.pl return $self; }
package B; require 'standard.pl'; sub new { # [snip] bless ($self, $class); do_something(); # subroutine in standard.pl return $self; }
# standard.pl # [snip] sub do_something { print "Howdy.\n"; } 1;
When I tried to run a script that used module A, I got an error message like this:
Undefined subroutine A::do_something called at A.pm line X.
I eventually realized that module B was requiring the same standard.pl library and changed the code in A to no longer require the standard.pl libary and the invocation of the do_something() routine from:
do_something();
to:
B::do_something();
... which worked. It made me feel a little deceptive, referring to a common subroutine in standard.pl as though it was part of module B, though. Why couldn't module A 'see' that do_something() subroutine in its own namespace, since I explicitly 'require'-d it in both modules?
Maybe the moral of the story is that I shouldn't be mixing packages and libraries, but I would like to understand this better in any case.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Burned by 'require'?
by phaylon (Curate) on Dec 28, 2005 at 23:37 UTC | |
|
Re: Burned by 'require'?
by choedebeck (Beadle) on Dec 28, 2005 at 23:07 UTC | |
|
Re: Burned by 'require'?
by Errto (Vicar) on Dec 29, 2005 at 04:39 UTC |