By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,903 Members | 1,086 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,903 IT Pros & Developers. It's quick & easy.

Can't locate object method "new" via package "A::B"

P: 9
I'm getting the following error:
Software error:
Can't locate object method "new" via package "A::B" at /path/file.cgi line 5.

My code is basically this:
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use warnings;
  4. use A::B;
  5. my $test = new A::B;
And in folder A in B.pm:
Expand|Select|Wrap|Line Numbers
  1. package A::B;
  2. use strict;
  3. use warnings;
  4.  
  5. sub new {
  6.     print "Content-type: text/html\n\nHello World!";
  7. }
  8. 1;
Obviously this isn't my real code, but I've simplified it. I didn't misspell the package name and I did define the 'new' method. Furthermore, I'm getting the error only when I test in the browser. I use Komodo IDE and it has a compiler and debugger and in Komodo it runs just fine. Also, my colleague (who's unfortunately away now) runs the same code and it's working for him.

I realize I'm not giving a lot of information here, but I don't know what else to specify except I'm on Mac OS X Leopard. I would really appreciate any help. If you need any other information, please ask for what you need to know.

Thank you!
Jul 23 '08 #1
Share this Question
Share on Google+
12 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
Hopefully your module code has more in it as well as it is missing things like the @EXPORT statements and such.

I think you need to read a good Perl module making tutorial. That one is pretty good and from an excellent site for that type of information.

Regards,

Jeff
Jul 23 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
You wouldn't be the first person that thought they were using the code they posted to find out they were mistaken. The main script is finding the A::B module, just not the method "new". So double-check that in the module A::B that the server you are using to run the main script from the browser is accessing the A::B module you think it is. If the "new" method is in the module and coded correctly I don't see why its not working for that simple example you posted.
Jul 23 '08 #3

P: 9
Thank you both very much for your answers! numberwhun: I'll be sure to check out your link sometimes.

I agree that it seems that it would seem that not the correct A::B module is accessed. However, I kind of have an idea that the code is right (so maybe I shouldn't have posted it), because the same code does work for my colleague.

The thing is: my boss decided that we needed to use some package that we can't get to work under Windows, so we had to switch to Mac (or Linux, but our boxes are all dual boot Mac/Windows). So now I have to get everything to work under Mac OS X. Since I think the code must be right, I'm looking more for an answer along the lines of "When you try to get Perl (modules) working under Mac OS X Leopard, you have to set environment variable XYZ to ABC, or maybe some configuration in Perl/CPAN/Apache/whatever". I'm not sure if it's something like that, and I'm both a noob at Perl and Mac OS X, but I figure that it should be something like that. (BTW, the @INC variable thing does include the right directories)

Thanks again for any answers!
Jul 25 '08 #4

P: 7
I donno if this is any help but you might need to include a path,

Expand|Select|Wrap|Line Numbers
  1. push @INC, "$ENV{'DOCUMENT_ROOT'}/someDirectory";
  2.  
The 'someDirectory' would be wherever you have your "A" directory located.

From my very limited experience... seems like if the server can't find "A" wherever it keeps its stuff then it quits unless you put additional paths to search in the @INC array. This might be a worthless post.
Jul 25 '08 #5

P: 9
Thanks for your answer. I tried that, but it didn't work. Also, I'm pretty sure the right path is in INC anyway. Let's say that B.pm is located in /path/A/B.pm, then /path was already in INC so A::B should be resolvable. I added /path/A with your method, but like I said it didn't help.

By the way, I looked in the Apache error_log and it prints another module in front of the error message:
C.pm: Can't locate object method "new" via package "A::B"
I'm very confused as to its role, because although C is loaded before I refer to A::B::new with a "do" statement, it does not link to A::B in any way. Could this provide some insight?
Jul 25 '08 #6

KevinADC
Expert 2.5K+
P: 4,059
Try changing the name of the module. It seems to find the module but not the function, so there might be a name conflict going on.
Jul 25 '08 #7

eWish
Expert 100+
P: 971
Here is link that should help you in creating your Perl Modules. I am certain that it will assist in resolving your issue.

--Kevin
Jul 26 '08 #8

P: 7
Thanks for your answer. I tried that, but it didn't work. Also, I'm pretty sure the right path is in INC anyway. Let's say that B.pm is located in /path/A/B.pm, then /path was already in INC so A::B should be resolvable. I added /path/A with your method, but like I said it didn't help.

By the way, I looked in the Apache error_log and it prints another module in front of the error message:
C.pm: Can't locate object method "new" via package "A::B"
I'm very confused as to its role, because although C is loaded before I refer to A::B::new with a "do" statement, it does not link to A::B in any way. Could this provide some insight?
You could try a real short test to check the value of @INC. Also check the value of DOCUMENT_ROOT, because some servers don't set it and in that case you have to spell out the full server path.

Maybe upload the following code as "test.cgi" and then call it up with the browswer and see whatcha got.
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. print "content-type: text/html\n\n";
  4. print "Value of INC is: @INC<br>\n";
  5. print "DOCUMENT_ROOT is: $ENV{'DOCUMENT_ROOT'}";
  6. exit;
  7.  
Unless the server path, all the way from the server root to your "A" directory, is in the @INC your script won't be found. On my server the @INC was pointed to nothing related to my site. I suggest not assuming anything about INC.

In my case my DOCUMENT_ROOT looks like this,
DOCUMENT_ROOT=/home/username/public_html

That gets me to my root directory, but my library isn't under my root so that isn't sufficient. I put my library under my cgi-bin directory, so I needed to set @INC to include that path, ie,
push @INC, "$ENV{'DOCUMENT_ROOT'}/cgi-bin/library";

I would then have the "A" directory under the "library" directory, and "B" under "A". Then the server can find "A::B".

If the DOCUMENT_ROOT variable was not set by the server I would have to set the @INC array like this,
push @INC, '/home/username/public_html/cgi-bin/library';

(be sure to use capital letters where indicated)

On more than one occasion it has come to my attention that I know less than everything. Therefore, I may be all wet on this matter, but I think your @INC is not pointing correctly. Anyway, printing the value of @INC will reveal the reality and leave one less mystery.
Jul 26 '08 #9

KevinADC
Expert 2.5K+
P: 4,059
He would be getting a different error message if the module could not be found at all somewhere in @INC. The module is found, but the "new" method/function is not. But your post could still be helpful for other reasons. Perl does have a pragma that is used to add stuff to @INC, the "lib" pragma:

Expand|Select|Wrap|Line Numbers
  1. use lib qw{path/to/mymodules};
.
Jul 26 '08 #10

P: 7
Kevin - thanks for the lib pragma.

So much information... so little brain.
Jul 26 '08 #11

KevinADC
Expert 2.5K+
P: 4,059
Kevin - thanks for the lib pragma.

So much information... so little brain.
hehehe.... I often fell the same way.
Jul 26 '08 #12

P: 9
I would like to thank all of you very much for taking the time to help me with my problem! I'm sorry I didn't participate in the discussion more actively over the past few days, but I only do this job part time and I tend not to think about it when I'm not at work here.

When I got to work today, everything seemed broken. I couldn't even connect to the localhost. It turned out that on the previous day I screwed up mod_perl (because I had a feeling that might have something to do with my problem) and that this was only noticeable when the server restarted, although there were no error messages whatsoever.
Anyway, there was a problem with mod_perl2, because even though I run a 64-bit OS (Mac OS X Leopard), things are compiled/installed for 32-bit OSes by default. This caused some files to not have the correct architecture. I'm not really sure why I got so few (and so apparently unrelated) error messages, but after solving the architecture problem, the page loads just fine.

For people with the same problem:
Before 'making' mod_perl2, you should make sure that ARCHFLAGS is set to include the 64-bit architectures:
export ARCHFLAGS='-arch i386 -arch x86_64 -arch ppc -arch ppc64'

Now you can install mod_perl like you normally would (see http://perl.apache.org/docs/2.0/user/intro/start_fast.html).

Also, I had a problem with XML::LibXML. You can find how you can install this (and maybe other modules?) for Leopard here: http://code.google.com/p/dotmac/wiki/LeopardInstallationGuide#Install_Perl_module_depen dencies

Thanks again for your help!
Jul 29 '08 #13

Post your reply

Sign in to post your reply or Sign up for a free account.