in reply to Writing a Perl Module... problems

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.

Replies are listed 'Best First'.
Re^2: Writing a Perl Module... problems
by cosmicperl (Chaplain) on Sep 15, 2007 at 03:01 UTC
    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!