Can't locate object method err

D

David Williams

Hello all,
I am getting the following error and need help. As a disclaimer,
I am heavily versed in php but not perl so if this is a simple error,
please forgive me:

Can't locate object method "new" via package "Net::Telnet" (perhaps you forgot to load "Net::Telnet"?) at ./check.pl line 5.
_


#!/usr/bin/perl
use strict;
use warnings;
use lib "/netmod/lib/site_perl/Net/";
my $t= new Net::Telnet('Telnet.pm') or die();

From the package, the syntax/spelling is correct:
package Net::Telnet

I installed the package in a non standard location because I do not
have root on the box. Telnet.pm is under

/netmod/lib/site_perl/Net/

but perl is acting like it cannot find it.
Anybody have any ideas?

thx,
David
--
 
J

Joost Diepenmaat

David Williams said:
Hello all,
I am getting the following error and need help. As a disclaimer,
I am heavily versed in php but not perl so if this is a simple error,
please forgive me:

Can't locate object method "new" via package "Net::Telnet" (perhaps
you forgot to load "Net::Telnet"?) at ./check.pl line 5.

The error is correct. You've not loaded Net::Telnet before you call the
new() method.
#!/usr/bin/perl
use strict;
use warnings;
use lib "/netmod/lib/site_perl/Net/";
my $t= new Net::Telnet('Telnet.pm') or die();

Net::Telnet's new() method does not take a 'Telnet.pm' argument.
Replace that last line with something like this:

use Net::Telnet;
my $t = Net::Telnet->new(
# your arguments here
);

As far as I can tell, the new() method should die() automatically if
anything goes wrong when connecting.
 
X

xhoster

David Williams said:
Hello all,
I am getting the following error and need help. As a disclaimer,
I am heavily versed in php but not perl so if this is a simple error,
please forgive me:

Can't locate object method "new" via package "Net::Telnet" (perhaps you
forgot to load "Net::Telnet"?) at ./check.pl line 5. _

I'm not sure why Perl doesn't just try to do an implicit "use %s" as
a last resort before failing with a 'perhaps you for forgot to lead "%s"'
message. It seems to me like it would be a nice convenience, but it
doesn't do that; and there is probably a reason that that would be a bad
idea that I have overlooked.

#!/usr/bin/perl
use strict;
use warnings;
use lib "/netmod/lib/site_perl/Net/";

This tells perl to add the above to the list of paths which are searched
to find the requested module. But you never tell perl to actually load any
module that might be found therein. Also, because the "Net" is part of the
module name and automatically gets added to each path when searching, it
probably shouldn't be explicitly included in the path, so:

use lib "/netmod/lib/site_perl/";


Then you need to actually load the module, by adding a line like:

use Net::Telnet;
my $t= new Net::Telnet('Telnet.pm') or die();

I don't know what the 'Telnet.pm' above is suppose to accomplish. I
suspect it shouldn't be there.
From the package, the syntax/spelling is correct:
package Net::Telnet

I installed the package in a non standard location because I do not
have root on the box. Telnet.pm is under

/netmod/lib/site_perl/Net/

but perl is acting like it cannot find it.
Anybody have any ideas?

You told perl where to search for a module, in case it needs to search for
one. But you never told it to actually do that search and load the module.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
J

Joost Diepenmaat

I'm not sure why Perl doesn't just try to do an implicit "use %s" as
a last resort before failing with a 'perhaps you for forgot to lead "%s"'
message. It seems to me like it would be a nice convenience, but it
doesn't do that; and there is probably a reason that that would be a bad
idea that I have overlooked.

Well, it's also likely that the package name is misspelled, and blindly
loading the wrong module could theoretically lead to much more
problematic issues than simply failing.
 
D

David Williams

Hi and thanks,
However,
I cannot use

use Net::Telnet;
because the module is not local.

so I put

use lib "netmod/lib/site_perl/Net";
which I thought loaded the module.
Is that not right?

David
Hello all,
I am getting the following error and need help. As a disclaimer,
I am heavily versed in php but not perl so if this is a simple error,
please forgive me:
Can't locate object method "new" via package "Net::Telnet" (perhaps you forgot to load "Net::Telnet"?) at ./check.pl line 5.
_

#!/usr/bin/perl
use strict;
use warnings;
use lib "/netmod/lib/site_perl/Net/";
my $t= new Net::Telnet('Telnet.pm') or die();
From the package, the syntax/spelling is correct:
package Net::Telnet
 
D

David Williams

Problem solved and thank you to all.

It came down to taking the Net off of the end of this string

#wrong
use lib '/netmod/lib/site_perl/Net';
#right
use lib '/netmod/lib/site_perl/';

and then actually using the module which I thought use lib did.
use Net::Telnet;

This is why I focused on php for 3 years and counting but
I need to learn perl clearly.
 
J

Joost Diepenmaat

David Williams said:
Hi and thanks,
However,
I cannot use

use Net::Telnet;
because the module is not local.

so I put

use lib "netmod/lib/site_perl/Net";
which I thought loaded the module.
Is that not right?

Not quite. You should do:

use lib "netmod/lib/site_perl/";
use Net::Telnet;

the 'use lib' statement adds the given *directory* to the @INC array
(this is a list of directories where modules should be loaded from).

the 'use Net::Telnet' command then searches for the 'Net/Telnet.pm' file
in the directories in the global @INC array. That way you can install as
many modules as you need to the "netmod/lib/site_perl" directory and
still use them without having to remember if they're locally or globally
installed.
 
U

Uri Guttman

A> in <URL:A> ,, (e-mail address removed) writes:
A> ,,
A> ,, > I'm not sure why Perl doesn't just try to do an implicit "use %s" as
A> ,, > a last resort before failing with a 'perhaps you for forgot to lead "%s"'
A> ,, > message. It seems to me like it would be a nice convenience, but it
A> ,, > doesn't do that; and there is probably a reason that that would be a bad
A> ,, > idea that I have overlooked.
A> ,,
A> ,, Well, it's also likely that the package name is misspelled, and blindly
A> ,, loading the wrong module could theoretically lead to much more
A> ,, problematic issues than simply failing.


A> Yeah, but one could have made said typo in the 'use' statement which could
A> have lead to problematic issues anyway.

A> Having said that, doing an implicite use statement sounds handy, and it
A> would have been if use statements never took arguments. But many times,
A> they do.

most of the time when passing args to use statements those are for
procedural imports, not OO. and missing classes in new calls are
obviously OO calls.

and there are other pitfalls to that idea. sometimes you load a module
with one package name and call new on another. a classic one is with use
IO::Socket but you call IO::Socket::INET->new.

also since there is no direct relationship between package and file
names (other than the implied import call in use), there is no way to
say if a missing class name is to be found in the module of the same
name. and since OO calls can be based on dynamic class names and classes
can be made on the fly, there are many ways to fail that will not be
fixed with an implied require call (shouldn't be a use since it is at
run time).

uri
 
J

Joost Diepenmaat

Abigail said:
Yeah, but one could have made said typo in the 'use' statement which could
have lead to problematic issues anyway.

Sure, but since the same package name is generally used at least as many
times in method calls as in "use" statements, the chances of mis-typing
it in one (of possibly many) method calls is higher.
Having said that, doing an implicite use statement sounds handy, and it
would have been if use statements never took arguments. But many times,
they do.

That's true. I hadn't even thought of that. Not to mention the modules
that initialize a whole set of classes from a single "use" statement.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top