Prolog

Let's say I'm importing function &foo from a package Foo:: into Bar:: .

That means that &foo will be aliased into the namespace of Bar:: . In code:

use Foo qw/foo/; => *Bar::foo = \&Foo::foo

Now when using Bar:: from Baz:: I have to be explicit about what I'm exporting to avoid &Bar::foo to be created too.°

So far so good...

Inheritance

But what if I'm using OOP and inheriting Bar from Baz?

Is there an easy way to avoid imported subs to show up in the chain?

I'm aware that adding a role to a class is analogous to a clever import of methods, but many standard modules are not meant to provide methods which are inherited. So the only way I can imagine to avoid such inheritance is call the full qualified sub instead of importing.

Apart from the extra clutter this is not practicable with all modules. And I doubt "normal" Perl programmers are aware about this.

What am I missing?

Demo:

Carp::cluck and Data::Dump::pp are only called explicitly and not imported.

But the imported Data::Dump::dd is "polluting" inheritance.

use strict; use warnings; use Carp; =head1 Demo class Bar =cut package Bar; use Data::Dump qw/dd/; sub bar { my ($self,@args) = @_; Carp::cluck Data::Dump::pp [$self->{args},@args]; } sub new { my ($class,@args) =@_; return bless {args => \@args}, $class; } sub AUTOLOAD { our $AUTOLOAD; warn "AUTOLOAD $AUTOLOAD"; } =head1 Proof Of Concept =cut package Baz; our @ISA = qw/Bar/; use Data::Dump qw/pp dd/; my $obj= Bar->new("is bar"); $obj->bar(0); warn "can dd: ",pp $obj->can("dd"); # available $obj->dd([1,2,3]); # will be executed

OUTPUT

[["is bar"], 0] at d:/exp/t_import_inheritance.pl line 17. Bar::bar(Bar=HASH(0x2654bb0), 0) called at d:/exp/t_import_inherit +ance.pl line 42 can dd: sub { ... } at d:/exp/t_import_inheritance.pl line 44. AUTOLOAD Bar::DESTROY at d:/exp/t_import_inheritance.pl line 27. (bless({ args => ["is bar"] }, "Bar"), [1, 2, 3])

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

°) i.o.W. Never Export All!!!


In reply to Not inheriting imported subs by LanX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.