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

Yo!

Say I have a base class, which I want another class to extend.

In that base class, I'd like to propagate a use'd lib into the class which is extending it... how would I do this?

Sorry, for some reason I"m really having a tough time figuring out how to word this to make sense...

Here's a really terse example which will hopefully make things clearer:

package Base; #use base qw( Carp ); # don't want to do that... use Carp; sub new { # do stuff, blah - you know the routine } package Foo; use base qw( Base ); # ... no good, don't wanna do '$self->carp' #sub some_method { my $self = shift; $self->carp("died") } # would rather just call it normaly, as if it were # use'd directly from within this package: sub some_method { my $self = shift; carp "died" }

Thanks!

Also, while I'm here - one other oop perl thing that's had me stumped for awhile.

So, I'm use'ing a really long-ass class path:

#!/usr/bin/perl use strict; use MyLibs::ForSome::Particular::Thing; # ugh! #my $fubar = MyLibs::ForSome::Particular::Thing->new() # how about: my $fubar = Thing->new(); # ???

Is it convieniently possible to do such a thing? It's a pet peeve of mine.

Beers!

Corey

Replies are listed 'Best First'.
Re: seamlessly use an extended class's use'd libs
by BrowserUk (Patriarch) on Sep 09, 2002 at 04:53 UTC

    If you add carp and your modules function(s) eg. Thing() to the @EXPORT global in your module, they will be exported into the namespace of anyone useing your module. However, this is not considered the "done thing" in most cases.

    The alternative is to add them to the @EXPORT_OK global. Then, anyone useing your module must explicitly name them on their use yourmodule qw(yourfunc carp); line in order that they become part of their namespace. So, given a module defined as:

    package Some::Really::Long; use Carp; use Exporter; @ISA=(Exporter); #@EXPORT=qw(Thing carp); # Method 1. @EXPORT_OK=qw(Thing carp); # Method 2. (preferred). $VERSION = 1.00; sub Thing{ return 'From Some::Really::Long::Thing()'; } 1;

    And a main program defined as

    #! perl -sw use strict; #use Some::Really::Long; # Method 1. use Some::Really::Long qw(Thing carp); # Method 2 (preferred). print Thing(),$/; carp "T'is a good day to die!\n";

    Using either method, the program gives:

    C:\test>test From Some::Really::Long::Thing() T'is a good day to die!
    I am not sure whether propogating use'd methods this way is "the done thing". It seems that it would be easier to just have the child class use Carp; if it needs to, rather than trying to propogate it down? I'm sure you'll get other (and better) opinions on this.
    Well It's better than the Abottoire, but Yorkshire!
Re: seamlessly use an extended class's use'd libs
by jkahn (Friar) on Sep 09, 2002 at 04:57 UTC
    Well, since the new class is its own package, you'll have to separately bring the Carp methods into that new class, by just adding the use Carp;
    Though this may seem like a pain, it's actually a good idea, since one of the main points of subclassing in OO coding is so that the extender need not know how the base class is implemented.
    If you really want to load all the packages that your base class has loaded, I suppose you could crawl the symbol tables yourself and load them all, but isn't it just easier to use the modules yourself? (Note: I've never done this, and it doesn't seem wise!)
    Perhaps you're concerned that loading the modules (with use) again would cost speed -- belay that fear. use is just require with a more sophisticated redundancy check.
Re: seamlessly use an extended class's use'd libs
by rir (Vicar) on Sep 09, 2002 at 05:41 UTC
    What incantation will you use to attract the package you wish?
    What signing will you do to repel the maleficent procedure?

    You have to do, to write, you can not yet code by the wish.

    You can look at AUTOLOAD, but this is a greedy solution.

    You can pollute UNIVERSAL with your foul needs.

    You poor nameless soul, sad for you, the nature, the power,
    the joy, of good naming is denied. The joy of many names
    lies far from you.

    *truename = \&Carp::truename;

    May this lead you to find one of your own true names.
    That is a tag, a signing, that refers to your true being.
    If not that, may it let you name those small things which
    are distant and in the naming bring them nigh.

    Your lib problem is the same wretchedness in another guise.
    You might try to

    use lib "/whereever/MyLibs/ForSome/Particular"; use Thing; my $thing = Thing->new();

    Or you might move your library. Or alias it by linking
    it beside MyLibs. These are operating system actions.

    Good luck. See you anon.

Re: seamlessly use an extended class's use'd libs
by Zaxo (Archbishop) on Sep 09, 2002 at 06:18 UTC

    If the Base:: namespace employs Carp properly, through import or explicit fully-qualified names, then Foo:: will do the same through inheritance. There is no need for Foo to use Carp; or even know about it.

    After Compline,
    Zaxo