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 +yNewPkg.pl 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 (Bishop) 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.