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

Please explain why I get here the error message Can't locate object method "new" via package "XXX"

use strict; use warnings; XXX->new(); package XXX; use Class::Struct; struct('field' => '$');
Class::Struct says about this way of using the struct function: "...assumes the current package name as the class name - so, why is XXX::new not recognized here?

-- 
Ronald Fischer <ynnor@mm.st>

Replies are listed 'Best First'.
Re: Class::Struct problem
by moritz (Cardinal) on Jun 08, 2010 at 12:44 UTC
    The struct(...) call is executed at run time, which is only after there call to XXX->new().

    So either make the call at compile time, or (recommended) do the construction of your classes first, either by putting it on the top of the code, or by writing it into a module and use it.

    Perl 6 - links to (nearly) everything that is Perl 6.
      either make the call at compile time
      Thanks a lot, this works (this is the solution I prefer in this particular case).

      -- 
      Ronald Fischer <ynnor@mm.st>
Re: Class::Struct problem
by okram (Monk) on Jun 08, 2010 at 12:44 UTC
    Would the following work for you?
    package XXX; use strict; use warnings; use Class::Struct; struct('field' => '$'); package main; use strict; use warnings; XXX->new();
      It would work, but I form the viewpoint of code organization, I would strongly prefer having the struct stuff near the end of my file instead of at the beginning.

      -- 
      Ronald Fischer <ynnor@mm.st>
        Just put your class inside a BEGIN block and put it wherever you want.