Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Can't locate object method 'new' ??

by isshino (Initiate)
on Aug 27, 2009 at 22:44 UTC ( #791765=perlquestion: print w/ replies, xml ) Need Help??
isshino has asked for the wisdom of the Perl Monks concerning the following question:

Hi I'm new to Perl and I'm trying to create an OO Module and I'm having trouble running it. I get the following error:

Can't locate object method "new" via package "testModule" at testFile.pl line 6

Here's my testModule file:

#!/usr/bin/perl package testModule; use strict; use warnings; sub new{ my $class = shift; my $self = {}; bless $self, $class; return $self; } sub printSomething{ return 'Something!'; } 1;

I used h2xs -XAn testModule to create a directory and then used:
Perl Makefile.PL
make
make install
to install the module. I'm using Perlv 5.8.8 on a macbook with MacOS V-10.5.7

Then I try to use the module on the following file:

#!/usr/bin/perl -w use strict; use testModule; my $ob = new testModule; print $ob->printSomething;

And I get the error I mentioned above when I try to run the test file. Any ideas why? there's certainly a new method on my testModule file. And I'm assuming it installed correctly because I'm using Komodo to edit the files, and it autocompletes the function printSomething on my testFile.

Any help would be greatly appreciated!

Comment on Can't locate object method 'new' ??
Select or Download Code
Re: Can't locate object method 'new' ??
by stevieb (Hermit) on Aug 27, 2009 at 23:15 UTC

    Does the name of your module file contain a suffix of ".pm"?

    When you look in your INC directories that are specified in the error message, does testModule.pm exist?

    Steve

    Update: if your module is a true module, and only contains a single package, then (IIRC) it shouldn't have the shebang line. Instead, the package declaration should be the first line in the file.

Re: Can't locate object method 'new' ??
by BrowserUk (Pope) on Aug 27, 2009 at 23:18 UTC
    And I'm assuming it installed correctly ...

    There's nothing wrong with the code:

    c:\test>copy con testModule.pm #!/usr/bin/perl package testModule; use strict; use warnings; sub new{ my $class = shift; my $self = {}; bless $self, $class; return $self; } sub printSomething{ return 'Something!'; } 1; ^Z 1 file(s) copied. c:\test>copy con testTestModule.pl #!/usr/bin/perl -w use strict; use testModule; my $ob = new testModule; print $ob->printSomething; ^Z 1 file(s) copied. c:\test>testTestModule.pl Something!

    Which only leaves an installation error. Wherever it is being installed, it would appear to be somewhere Komodo can find it, but your (default?) Perl installation cannot.


    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.

      Ok, I'm going to try installing it on my server to see if an error that has to do with the way I'm installing it.

      Already tried without the first line and everything was the same. And yes, it has a suffix .pm

      Thanks for the help

Re: Can't locate object method 'new' ??
by perlkiller (Acolyte) on Aug 27, 2009 at 23:23 UTC

    delete this line from the module:

    #!/usr/bin/perl

      It's a comment. What do you expect deleting that to help?

        That line is not a comment. In Unix, it is a start for perl script.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2014-10-25 19:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (148 votes), past polls