http://qs321.pair.com?node_id=836566


in reply to Re^3: Simple inheritance question
in thread Simple inheritance question

Yeah, $self->test will work (altho this still does not require a constructor for One, seems like a red herring):
#!/usr/bin/perl -w use strict; { package One; sub test { my $x = pop; print "$x\n"; } } { package Two; use base "One"; sub new { my $self = {}; bless($self); } sub eg { my $self = shift; $self->test(pop); } } my $obj = new Two(); $obj->eg("hello");
I guess this is starting to seem a little finnicky, but I would like to use test() as if it were a function internal to Two, not requiring an object.

Basically, I have two classes that share a bunch of such internal functions (no object required, it's just string parsing), so I thought the best thing would be to have a base class containing those.

In fact Exporter does work, see my reply to jethro.

Replies are listed 'Best First'.
Re^5: Simple inheritance question
by BrowserUk (Patriarch) on Apr 23, 2010 at 21:09 UTC
    Basically, I have two classes that share a bunch of such internal functions (no object required, it's just string parsing)

    Put the functions into a third, non-class module, export them, and use it in both your class modules.

    Inheritance only works for methods, not plain ol' functions. And inheriting from a class that you've no intention of using inheritance--just so you can import some functions that live in the same file--leaves you open to all kinds of nasty surprises down the line.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re^5: Simple inheritance question
by ikegami (Patriarch) on Apr 23, 2010 at 20:40 UTC

    as if it were a function internal to Two, not requiring an object.

    As if it's something it isn't. So make it what you want it to be.

    BEGIN { *Two::test = \&One::test; }

    It's really weird to export functions from a Class module, though.

Re^5: Simple inheritance question
by webfiend (Vicar) on Apr 23, 2010 at 17:12 UTC

    Glad you got the solution you needed. I was a little unclear on the original question. I also think it's interesting that One doesn't need a constructor in the object solution. It makes sense, considering that a One is never instantiated. I didn't realize that Perl's package handling let's you "inherit" from a package that isn't actually a class.

    We learn something every day if we give ourselves a chance.

      I didn't realize that Perl's package handling let's you "inherit" from a package that isn't actually a class.

      There's no effective difference between a package and a class or a function and a method inside Perl 5. The internals don't care. If you treat something like a class name (by using it as an invocant), Perl 5 will use that package's namespace to find the destination method. Any reference blessed into a package will use that package's namespace to find its destination methods.

      Those packages don't even have to exist for Perl 5 to attempt to dispatch to them as invocants, but you will get dispatch errors if they don't.