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

What can cause:
Can't locate object method "new" via package "x::y::z" (perhaps you forgot to load "x::y::z"?

in response to:
my $foo = x::y::z->new(...);

when you can plainly see that the module in question does contain:
use x::y::z;

and that the file x/y/z.pm includes a proper package statement:   (all examples match WRT case-sensitivity, etc.)
package x::y::z;

?

This is a case-insensitive filesystem and the module in question can be compiled correctly and can be used.   There are no CaSe MiSmAtCh problems.   There is no hanky-panky going on with @INC or anything like that.   I can, in a command line one-liner, 'use x::y::z; my $foo=x::y::z->new();' and it works.

I know that similar issues have been discussed in the recent past, but this does not seem to be quite the same thing.   Is there a previous posting that anyone can point me to on this one?   I am genuinely puzzled as to what Perl is trying to tell me.

Replies are listed 'Best First'.
Re: "Can't locate object method 'new' but the package IS 'used'
by kennethk (Abbot) on Jun 14, 2011 at 18:40 UTC
    Do your modules use each other (circularity)? I've seen similar things happen when you attempt to use methods prior to them being defined in the compile phase. I get a similar error with:

    Foo.pm

    package Foo; use Bar; sub new { return bless {}, shift; } 1;

    Bar.pm:

    package Bar; use Foo; sub new { return bless {}, shift; } my $foo = Foo->new(); 1;

    script.pl:

    #!/usr/bin/perl use strict; use warnings; use Foo;
Re: "Can't locate object method 'new' but the package IS 'used'
by ig (Vicar) on Jun 14, 2011 at 18:56 UTC

    You might try inspecting your symbol tables (Devel::Symdump or the like).

    Maybe you have some non-printing character(s) somewhere? I have had, after cut-and-paste operations.

Re: "Can't locate object method 'new' but the package IS 'used'
by Anonymous Monk on Jun 14, 2011 at 22:12 UTC

      If I could do that, I wouldn’ be askin’, eh?

        :-/
        If I could do that, I wouldn’ be askin’, eh?

        Why not? You start deleting code until you have a minimal example that demonstrates the problem