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...
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?
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |