LanX has asked for the wisdom of the Perl Monks concerning the following question:

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!!!

Replies are listed 'Best First'.
Re: Not inheriting imported subs
by choroba (Cardinal) on Apr 28, 2019 at 20:53 UTC
    That's what namespace::clean or namespace::autoclean do.

    In your example, just add

    use namespace::clean;
    right after
    use Data::Dump qw/dd/;
    in the Bar package.

    can dd: undef at ./1.pl line 45. AUTOLOAD Bar::dd at ./1.pl line 27.
    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      Great! Finally I get what namespace::clean does.

      Another good reason to buy Ribasushi a beer or comb his hair ... or both! :)

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

Re: Not inheriting imported subs
by karlgoethebier (Abbot) on Apr 29, 2019 at 13:13 UTC
    "...What am I missing?..."

    I guess you miss nothing. But don't you want a kind of proxy? I'm not sure if this is really helpful and if i understood you right - and sometimes my mind is a bit free-floating.

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

      no, I already mentioned the poor man's solution you got in that thread. (which is appropriate in that case)

      The point is one doesn't want to always call only fully::qualified::subs().

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

        "... the poor man's solution..."

        Easy said. I didn't overlook what you said. But the construct mentioned is a little bit different. And simplicity is one of the hardest things to accomplish.

        "... one doesn't want to always call only fully::qualified::subs()."

        Why not if these bloody details are hidden in some role?

        Best regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

        perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help