Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^2: Inherit parent, AND also import parent's exported sub ??

by exilepanda (Pilgrim)
on Sep 08, 2016 at 19:53 UTC ( #1171431=note: print w/replies, xml ) Need Help??


in reply to Re: Inherit parent, AND also import parent's exported sub ??
in thread Inherit parent, AND also import parent's exported sub ??

Umm.. sorry perhaps I didn't make my question clear enough. But take your sample as an example:
use warnings; use strict; package One; require Exporter; # added our @ISA =qw/Exporter/;# added our @EXPORT = qw/foo/;# added sub foo { print "I'm One::foo >> @_ \n"; # changed } package Two; use base 'One'; sub new { return bless {}, shift; } sub foo { my $self = shift; print "I'm Two::foo(). I'm going to call " . "my Parent's foo()...\n"; $self->SUPER::foo(); } package main; my $obj = Two->new; $obj->foo("abc"); # Now foo() has an extra first element in @_ foo("abc"); # die() for Bareword from 'strict subs'
In this case foo() was not created/designed as a method in One, but an exported function. From my understanding, foo() is only visible in Two, but not main::. Actually, I can do that like :
package Two; use base 'One'; require Exporter; our @EXPORT = qw/foo/; *foo = \&One::foo; sub new { ... } sub newMethod { ... } sub overrideOldMethod { ... }
But then, I have to declare every function from the base class manually, and if I sub class Two later, I have to do that again. So, is that anyway I can having both inherit the base class (methods), and also have their exported functions at once?

Replies are listed 'Best First'.
Re^3: Inherit parent, AND also import parent's exported sub ??
by Anonymous Monk on Sep 08, 2016 at 20:39 UTC

    Read Exporter

    use base 'One'; use One '/.+/';

    OO classes that export lots of stuff ... code smell :)

      Seem to work but when I subclass Two, that function just gone again... Seem I should end this in my first subclass by:
      sub nameOfOldFunc { shift; return OldClass::nameOfOldFunc ( @_ ) ; }
      making it a method, and this become a method ever after.

      Thank you very much for the help!

        Any chance you could share some of your real code that demonstrates what you're attempting to do? What you've suggested here is not scalable, and a bit confusing.

        Perhaps if we understood the real problem you're facing with real example code, we could provide better solutions to your dilemma.

        Having both inherited and imported subs coming from the same module is a bit unusual, unless you're intentionally directing your API to both OO and functional crowds, or providing backwards-compatibility (there are other scenarios, but I digress).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1171431]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2020-02-21 13:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (96 votes). Check out past polls.

    Notices?