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

Hi All,
  I'm working on a new Perl module, but I'm having trouble getting a couple of the features working. I've checked in my Advanced Perl programming book, but I can't figure it out. I want to call the module functions as:-
Name::Name->subroutine()
But at the moment the only way I can get the subroutines to run is:-
Name::Name::subroutine()

A cut down version of the module so (hopefully) you can tell me what's missing:-
package Name::Name; use strict; use vars qw($GLOBALS); BEGIN { require Exporter; @ISA = qw(Exporter); $Name::Name::VERSION = '0.05'; } sub new { my $class = shift; my $self = {}; bless $self,$class; my %input = @_; ## BLAH } 1;
I've had a look at other scripts that can call routines in this way, such a Imager, but just can't figure out what I need to do. Please help.

Thank you
Lyle Hopkins

Replies are listed 'Best First'.
Re: Writing a Perl Module... problems
by Joost (Canon) on Sep 15, 2007 at 01:29 UTC
    Nothing is wrong except for your probably misguided mixing of Exporter and OO code.

    Your code should work as planned when called as Name::Name->new()

    Class->method() works differently from Class::method only in that:

    1) Class->method(@args) respects inheritance.

    2) Class->method(@args) inserts "Class" as the first argument while Class::method() does not.

    update: most people expect new() to be the constructor so it should return $self.

      Your right it does work now. I'm not sure why it wasn't working before, more annoyingly I can't remember what error I was getting. I made a few changes before trying again and it works as I originally planner. How strange, there must of been something I changed. If I get the error again I'll post it here for future ref by anyone who reads this thread.
        With Name::Name->subroutine() you need to capture the class name as the first argument in subroutine() method.
        package Name::Name; sub subroutine { my $class = shift; # shifting @_ print $class, "\n"; } package main; use Name::Name; Name::Name->subroutine(); # output: # Name::Name
        It's callled class method calling. With Name::Name::subroutine(), you call subroutine() with fully qualified package name without any argument.

        I'm not sure what's missing, but if the subroutine in question is new() then you can't call it Name::Name::new() as it expects the class name as its first argument. Similiar way to the class method calling above is Name::Name::new('Name::Name').


        Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Re: Writing a Perl Module... problems
by dragonchild (Archbishop) on Sep 15, 2007 at 03:17 UTC
    A couple improvements to your general writing style:
    package Name::Name; use 5.6.0; # Always specify the minimum Perl your code expects to work + with. use strict; # Got this right. use warnings FATAL => 'all'; # If you're 5.6+, this catches a lot of s +illy mistakes. our $VERSION = '0.05'; # If you're 5.6+, this is a nicer way of writin +g that VERSION stuff. # MY CODE HERE 1; __END__ # Always put this here to tell you that there can never be any + code after this.
    That happens to be the template for whenever I start a new .pm or .pl file in vim (minus the comments, of course).

    Now - why do you want to call the methods Foo->meth() vs. having it be a library of functions? I've always thought that the way File::Spec does this is really freaking annoying. If you're a library of functions, then provide the functions for export. If you're an OO module, then use Moose (or any of the other 1235123123 OO modules on CPAN) to provide the OO stuff, have your users receive an object, and call methods on that. Just because you can do something is a poor reason to actually do it, especially in production. KISS is a good motto.


    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
      I've added use '5.004'. I Already had __END__ (in my version anyway). I wont use 'our', I used it in the past and hit problems when installing my scripts on machines with older copies of Perl. Now I stick to 'use vars'.

      The module users do receive an object they call methods on.
      use 5.6.0;

      Just FYI - This actually breaks in 5.10, it should be 5.006. Here is what the docs say about it.

      v-string in use/require is non-portable
      (W portable) The use of v-strings is non-portable to older, pre-5.6, Perls. If you want your scripts to be backward portable, use the floating point version number: for example, instead of "use 5.6.1" say "use 5.006_001". This of course won't help: the older Perls won't suddenly start understanding newer features, but at least they will show a sensible error message indicating the required minimum version.

      -stvn
Re: Writing a Perl Module... problems
by runrig (Abbot) on Sep 15, 2007 at 01:27 UTC
    Read some documentation. Maybe start with perlboot, perltoot, and bless. When you call a subroutine with Class->method(...) or $object->method(...), the first argument is the class name or object itself. Oh, and did I mention, "read the docs"??
      Reading them now, would have read them in the first place if I knew which ones were relevant to what I'm trying to do.