Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

General module question

by jbeninger (Monk)
on Oct 07, 2002 at 17:56 UTC ( #203437=perlquestion: print w/ replies, xml ) Need Help??
jbeninger has asked for the wisdom of the Perl Monks concerning the following question:

Hi there. I have two problems I'm experiencing with modules at the moment. The first - and smaller - problem is this. I have a file, Foo.pm, that contains something like this :

package Foo::Bar; sub s1 {} sub s2 {} ... package Foo::Bar::Subclass1; use base Foo::Bar; ... package Foo::Bar::Subclass2; use base Foo::Bar;

I'd like to call Foo::Bar::s1 from any of the subclasses. But - I don't want to call them as class methods (ie $this->s1()). The motivation behind this is that s1, s2, etc aren't really directly related to Foo::Bar or its subclasses, but will be used by all of the subclasses as helper functions at some point.

I've tried using Foo::Bar::s1() from Foo::Bar::Subclass1 - but I get an error saying it could not be found. I *think* this is because the file is called Foo.pm, and not Foo/Bar.pm - but once again, I'm not sure. I've got it working right now by calling s1 from $this, but it seems to me that using $this->s1() for a function that never uses $this is inelegant. If there's an easy way to fix this, let me know.

Second Problem
Of course, the above problem stems from a more deeply-rooted problem - I don't know that much about the inner workings of modules and inheritance. I've tried to RTFM, but there's so much documentation out there, I only find snippets and bits in any place I look. Could anyone point me to a good resource about exactly how modules work? I've read what appeared to be the pertinent man pages (perltoot and perlmod, for instance), but they just provided enough to be usable, not the monk-like knowledge I just *know* is floating around there, somewhere.

Thanks!
James

Comment on General module question
Download Code
Re: General module question
by VSarkiss (Monsignor) on Oct 07, 2002 at 18:56 UTC

    If they're class methods (i.e., not instance methods), you can call them as such. For instance: Foo::Bar::Subclass1->s1();
    will call the s1 you've defined in Foo::Bar unless you override it with a similarly named method in Foo::Bar::Subclass1. In either case, the name of the class that it is invoked from will be passed in as the first argument (instead of a reference to the instance).

    If you've already read the docs and they're not satisfactory, I'd suggest reading Damian Conway's excellent book Object-Oriented Perl. It's not introductory, but it's very thorough.

Re: General module question
by chromatic (Archbishop) on Oct 07, 2002 at 19:35 UTC
    I've tried using Foo::Bar::s1() from Foo::Bar::Subclass1 - but I get an error saying it could not be found. I *think* this is because the file is called Foo.pm, and not Foo/Bar.pm

    I think you're on the right track here, but I'm not completely sure. The base pragma should complain if it didn't find a module with the appropriate name. Of course, you can define a package of any name in a file of any valid name -- nothing says that Foo::Bar has to be defined in Foo/Bar.pm. You'll just have to go through contortions to load it properly, and that's where I'd expect use base to complain.

    I'd recommend fixing the file name, as that may clear up the issue.

      The thing is, I'm writing YAHTMLM (Yet Another HTML Module), and have items like "FormManager::Component::Select", "FormManager::Component::Text" - all inside FormManager.pm. I haven't had any trouble using the module so far except for this one bug. I'd like to have them all loaded with FormManager, so you get a whole bunch of default components without having to 'use' each one. There may be a better way, but see problem 2 :)
Re: General module question
by kabel (Chaplain) on Oct 07, 2002 at 22:13 UTC
    you write that s1 () and s2 () are methods that are not bound to one specific namespace. i suggest you put this two functions into an separated one - called for example foo::bar::helper or whatsoever.

    that underlines the aspect that the functions do not belong originally to foo::bar.
Re: General module question
by strider corinth (Friar) on Oct 08, 2002 at 01:49 UTC
    If your modules are descended from your base class, you can call those methods without specifying their namespace at all:
    package Foo::Bar; sub s1 {} sub s2 {} ... package Foo::Bar::Subclass1; use base Foo::Bar; s1(); ... package Foo::Bar::Subclass2; use base Foo::Bar; s1(); s2();
    Just make sure s1() and s2() aren't shifting $self (or whatever you call it) off of @_ first, and they'll act like built in functions. That may be closer to the behavior you're looking for anyway, since they don't really 'belong to' any of the packages, but are used by them all.

    --

    Love justice; desire mercy.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://203437]
Approved by Aristotle
Front-paged by astaines
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2014-11-29 06:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (203 votes), past polls