Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Can't locate object method "new" via package MyNewPkg ...

by Plankton (Vicar)
on Oct 05, 2007 at 17:02 UTC ( #642962=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks, I need to ape a module of some existing code. I thought I could just get away with copying a existing module and change a few things in it and away I go, but I hit a snag ... I keep getting ...
Can't locate object method "new" via package "XYZ::ABC::Role::MyNewPkg +" (perhaps you forgot to load "XYZ::ABC::Role::MyNewPkg"?) at ./testM line 6.
... error messages. My script test script looks like this ...
#!perl use strict; use XYZ::ABC::Role::MyNewPkg; my $pkg = XYZ::ABC::Role::MyNewPkg->new(); print "okay\n";
What am I doing wrong here?

Replies are listed 'Best First'.
Re: Can't locate object method "new" via package MyNewPkg ...
by jhourcle (Prior) on Oct 05, 2007 at 17:11 UTC

    First thing off the top of my head:

    Check the 'package' line in your module, and make sure that it _exactly_ matches (including case) the name in the 'use' statement.

    I typically run into this after I've decided to rearrange some files, or I typo on a case-insensitive file system (so it finds the module, but the name doesn't match)

      Doh! That was it thanks! ... I have come to realize... I can deny it no longer! I am small.
Re: Can't locate object method "new" via package MyNewPkg ...
by Fletch (Chancellor) on Oct 05, 2007 at 17:09 UTC

    Well, you're calling a method new in a package which doesn't implement that method. (Just like the error says; strange how that sometimes works . . . )

    Look at the documentation (or baring that, the source) and see what methods it implements.

    Update: Aaah, rereading again it looks like you may be trying to make your own module which has the same contents as another. Check that you also updated the package declaration to match the new module (and file) name.

    If you don't do that the subs will be declared in the original package despite the filename having been changed. Just remember that the perl package namespace code gets puts into does not have to correspond to the package name you'd build from the filename.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2020-07-06 03:06 GMT
Find Nodes?
    Voting Booth?

    No recent polls found