Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Can't locate object method "new" via package ... perhaps you forgot to load...Oh yes I did!

by Phemto (Acolyte)
on Jan 27, 2011 at 23:32 UTC ( [id://884686]=perlquestion: print w/replies, xml ) Need Help??

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

Ok, rookie question, but it has me totally stumped. Working in Komodo 5.2.4

I have a package and a program in the same directory. Here is the start of the package Syringe.pm .

#Package Syringe package Syringe; use strict; use warnings; use MiscGeo; use Math::ConvexHull; $VERSION = 0.9; @ISA = qw(Exporter); @EXPORT = qw(new increment populate Particle_Path Convex_Hull); @EXPORT_OK = qw(net_velocity); use constant PI => 4 * atan2 1, 1; sub new { my $class = shift; my $self = {}; bless $self, $class; ...

And here is the start of the program

#!/usr/local/ActivePerl-5.10/bin/perl -w #This program uses the Syringe module (if I can get it to recognize it +) to model #the movement of particles in a rotating syringe. use strict; use warnings; use Syringe; use constant PI => 4 * atan2 1, 1; my $time_step = 0.1; #s my $syringe = Syringe->new(); ...

The program is apparently finding the module, but not the methods. As far as I can tell (via PPM) there is no name collision with existing modules. I've written plenty of modules before, and I don't see what I'm doing differently here, other than banging my head against the wall.

  • Comment on Can't locate object method "new" via package ... perhaps you forgot to load...Oh yes I did!
  • Select or Download Code

Replies are listed 'Best First'.
Re: Can't locate object method "new" via package ... perhaps you forgot to load...Oh yes I did!
by mr_mischief (Monsignor) on Jan 27, 2011 at 23:49 UTC

    I won't comment on the problem with finding the methods, but I will point out something else that seems quite odd. Why are you exporting the names of methods as functional sub names to your main program namespace? This is odd and potentially causes strange and annoying bugs (especially by exporting new). There's no need to clutter your main program's namespace if you're only going to call these as methods.

Re: Can't locate object method "new" via package ... perhaps you forgot to load...Oh yes I did!
by GrandFather (Saint) on Jan 27, 2011 at 23:55 UTC

    I can't see how it relates to the error you describe unless there are earlier errors you don't describe, but the line:

    $VERSION = 0.9;

    in the Syringe code shouldn't compile. That would prevent Syringe loading and would generate compile errors (but not the error you describe).

    Failing that being helpful you'll need to provide enough code to reproduce the problem. I suggest removing everything from Syringe that doesn't affect the problem and if that doesn't turn on a light for you post the result here. Note in particular that you should remove references to other modules first!

    True laziness is hard work
      Maybe he's not loading the file he think he is loading. Inspecting $INC{'Syringe.pm'} will show what file was loaded.
Re: Can't locate object method "new" via package ... perhaps you forgot to load...Oh yes I did!
by Phemto (Acolyte) on Jan 28, 2011 at 09:58 UTC

    Thanks for the input everyone. The @EXPORT was a desparate attempt to fix the problem. I should have taken it back out. Here are the complete program and module in a more minimalist form.

    Here is syringe.pl

    #!/usr/local/ActivePerl-5.10/bin/perl -w use strict; use warnings; use Syringe; print $INC{"Syringe.pm"}; my $syringe = Syringe->new(); print "Wohooooo!";

    and here is syringe.pm, which is in the same directory.

    #Package Syringe package Syringe; use strict; use warnings; @ISA = qw(Exporter); @EXPORT = qw(); @EXPORT_OK = qw(); use constant PI => 4 * atan2 1, 1; sub new { my $class = shift; my $self = {}; bless $self, $class; $self->{picture_width} = $args{picture_width}; return $self; } 1;

    When I run it, I get the following results. The "use lib" line is not strictly necessary, but it forces the full module path to be printed, which assures me that there isn't some other Syringe.pm that I'm loading. The result is essentially the same as before. I'd be tempted to blame some misconfiguration in the Komodo environment, but I have other module/program pairs that I can run without problems.

    Can't locate object method "new" via package "Syringe" (perhaps you fo +rgot to load "Syringe"?) at /Users/phowell/Documents/Work/Programming +/Syringe/Syringe.pl line 8. /Users/phowell/Documents/Work/Programming/Syringe/Syringe.pm

      Your file Syringe.pm still contains errors that strict.pm cannot let pass. So you are still loading the wrong Syringe.pm file (or loading the right one but modifying/posting the wrong one).

      First test that Syringe.pm compiles well:

      perl -Ilib -MSyringe -e1

      If this command raises any errors, then they are in Syringe.pm.

      Then, go on to put this into a (very small) Perl script:

      #!perl -w use strict; use Syringe; print "OK\n";

      Get this script to run.

      After that, continue to add stuff until it breaks again (but by then, it shouldn't).

      When debugging some "magic module loader" stuff (which I've since come to abhor, for the ugly things hidden by magic module loading), I've often resorted to putting BEGIN blocks into the modules that output "I am here" messages:

      package My::Module; BEGIN { print "Loading " . __PACKAGE__ . " from " . __FILE__ . "\n"}; ... the methods in My::Module ... print "Loaded " . __PACKAGE__ . " from " . __FILE__ . "\n"; 1; __END__

      This spews a lot of messages, but on the upside, you get a trace of what module gets loaded from where (and when). If there is a "Loading..." message, but no corresponding "Loaded..." message, then you know that there was an error and something along the way gobbled up and hid that error instead of stopping execution.

      Your description is not consistent with the code you provide! Syringe.pm does not compile due to the exporter related lines and due to the $self->{picture_width} = $args{picture_width}; line.

      The output from the print $INC{"Syringe.pm"}; line is consistent with a file called Syringe.pm being found in the same directory as the .pl file so the implication is that the file being loaded is not the one you think it is. Have you two files with different case perhaps, or maybe the file you are editing is in a different directory than the .pl file, but there is an old version of the .pm that is being found?

      True laziness is hard work

        GrandFather gets the cigar - along with everyone else who told me that it couldn't be the same file. When I followed his advice and stopped trusting the IDE, and went back to the terminal, it was obvious. That explains why I wasn't seeing the compilation errors, even when I "asked for them."

        I hunted down the files that were getting loaded, moved them to my working directory, and made sure the IDE was pointing to them. Now I'm getting lots of reassuring syntax errors.

        My lesson: It's not true laziness to depend on the IDE. When in doubt, drop to the command line and make sure you really know whats going on.

        Thanks Folks!

      is it syringe.pm or Syringe.pm? There's a difference.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-20 03:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found