There is no "safe" import. Your best defense is to keep in the forefront of your mind that in Perl, a "method" is nothing more than a subroutine. Consequently,
any subroutine that you import into a file, becomes part of that package's name space and hence a method of any object blessed into that package.
More specifically, the statement use B qw(moo) adds "moo" to the A namespace, thus defining A::moo(...) - this means that class A now has its own definition moo that overrides anything it inherits from C. Thus when you called A->new the code with variables resolved looked something like this:
my $self=bless({},"A");
$self->moo; #equivalent to $self->A::moo
A::moo(...) just happens to be an alias for B::moo(...) so you get the print out "B::moo" rather than "C::moo".
The lesson: if you don't want subroutine "foo" to magically become part of class "baz", don't import it into "baz"'s namespace. The best way to prevent this is to follow Almut's advice and use the incantation use B () whenever you import a class, unless you know that it is well behaved and doesn't auto-export anything.
Of course, if you want "blah::foo" to be a method of Class "baz", you can call use blah qw(foo) without fear.
Best, beth
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.