473,395 Members | 2,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
12 18198
numberwhun
3,509 Expert Mod 2GB
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
4,059 Expert 2GB
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
Jordi
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
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
Jordi
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
4,059 Expert 2GB
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
971 Expert 512MB
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
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
4,059 Expert 2GB
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
Kevin - thanks for the lib pragma.

So much information... so little brain.
Jul 26 '08 #11
KevinADC
4,059 Expert 2GB
Kevin - thanks for the lib pragma.

So much information... so little brain.
hehehe.... I often fell the same way.
Jul 26 '08 #12
Jordi
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

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

Similar topics

1
by: alanrn | last post by:
I've implemented a number of strongly-typed collections that inherit from CollectionBase and recently noticed something that I don't fully understand. CollectionBase defines method RemoveAt(). ...
4
by: seesaw | last post by:
class A { public: static A* newA() { return new A; } .... }; In the code, two things not very clear and natural to me: 1. the method newA() is defined as static. 2. newA as a member method...
24
by: Rv5 | last post by:
Rookie c++ question, but Ive spent the last 5 years doing Java, where everytime I created an object I used new. In c++ I can create my objects without and its confusing me just a little. I have...
8
by: Dot net work | last post by:
I need VB.NET's "shadows" functionality inside a C# project. I tried the "new" keyword, but it didn't seem to work, because my particular function does in fact differ in signature to the function...
5
by: Sam | last post by:
Hi everyone Could anyone help me understand the usage of the "New" keyword I'm new to VB.Net. 1. Why do we use the "New" keyword on some object type variables such as the myPen of the...
24
by: M O J O | last post by:
Hi, Instead of doing this.... Public Class Form1 Public Shared Sub CreateAndShow() Dim f As New Form1 f.Show() End Sub
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
3
blazedaces
by: blazedaces | last post by:
Hello, it's been a while since I posted on these forums. My issue I think isn't as much in my code as it's in the syntax and structure in references other packages. I'll get straight to it then: ...
30
by: Medvedev | last post by:
i see serveral source codes , and i found they almost only use "new" and "delete" keywords to make they object. Why should i do that , and as i know the object is going to be destroy by itself at...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.