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

Hi I'm getting a problem when i run my perl script
#!usr/bin/perl use Net::Telnet (); $telnet = new Net::Telnet ( Timeout=>10,Errmode=>'die'); $telnet->open('192.168.1.1'); $telnet->waitfor('/login: $/i'); $telnet->print('root'); $telnet->waitfor('/password: $/i'); $telnet->print('root'); $telnet->waitfor('/\$ $/i'); $telnet->print('who'); $output = $telnet->waitfor('/\$ $/i'); print $output;
The Problem ---->> ( when i run it) root@localhost root# perl test Can't locate object method "new" via package "Net::Telnet" at test line 19. any suggestions ??? chubby

Replies are listed 'Best First'.
Re: Can't locate object method
by broquaint (Abbot) on Jun 24, 2002 at 11:55 UTC
    As agoth has already suggested this error is probably due to the fact that Net::Telnet is either outside the perl library path or not on the machine at all. Check out the the perldiag manpage for more information about error messages, or better yet use diagnostics with strict and warnings.

    Also watch out for indirect method syntax (which is new Net::Telnet @args as opposed to direct syntax like Net::Telnet->new(@args)[1]) as it's liable to trip you up if you're not exactly sure what it's doing. Here's an example of where it can be fatal

    package foo; sub new { print "I'm in foo::new()\n"; bless {}, 'foo'; } 1; package main; sub foo { print "I'm in main::foo()\n"; } my $o = new foo; __output__ Bareword found where operator expected at - line 17, near "new foo" (Do you need to predeclare new?) syntax error at - line 17, near "new foo"
    This is a particularly pathological case but it's something to be wary of all the same.
    HTH

    _________
    broquaint

    [1] there's even an argument to use the very explicit Net::Telnet::->new but I don't think I've seen a case yet where it's necessary ...

      Hah. broquaint++ The indirect syntax is a recipie for error (too bad so many people (including luminaries) come from a C++ background and perpetuate the syntax in CPAN module documentation.)

      Another exaple of bizarreness that I came across was this:

      my $obj=new Foo::Bar->new('parameter');
      When using a dual purpose new (as in it works on classes and objects) the above wont raise an error and you may find yourself wondering why 'parameter' hasnt been passed to the returned object.

      Of course your example is more likeyly to happen.... Alas. :-)

      Yves / DeMerphq
      ---
      Writing a good benchmark isnt as easy as it might look.

Re: Can't locate object method
by bronto (Priest) on Jun 24, 2002 at 11:55 UTC

    Erm... your code runs here.

    bronto@cooper:/tmp$ perl test.pl problem connecting to "192.168.1.1", port 23: connection timed-out at +test.pl line 5

    Hence your code works (or it is likely to work). So the question is: have you Net::Telnet properly installed?

    --bronto

    PS: BTW, if you don't know how to check if you have it, try perl -MNet::Telnet -e 'exit 0', and watch errors in case it dies: they will tell.

    PS2: (or PS/2 :-) if you don't have it, just perl -MCPAN -e 'install Net::Telnet', then retry your script.


    update: are you sure you don't have a "fake" Net::Telnet in the way?

Re: Can't locate object method
by Sifmole (Chaplain) on Jun 24, 2002 at 11:16 UTC
    use warnings;
    use strict;

    You will likely see the problem very quickly. Additionally your pound-bang (#!) seems like it might be incorrect, although you are apparently able to run it so it could be right and just different looking.

Re: Can't locate object method
by agoth (Chaplain) on Jun 24, 2002 at 11:13 UTC
    I would suggest that the object creation is failing because the PERL5LIB or perl path isnt set correctly for the root user.

    PS. Telnet as root isn't too clever a thing to be doing.

Re: Can't locate object method
by smitz (Chaplain) on Jun 24, 2002 at 11:48 UTC
    Replace
    use Net::Telnet ();

    with
    use Net::Telnet;

    SMiTZ

      ... but Net::Telnet doesn't export anything by default, especially new()! (I'm looking at version 3.02). What did you expect this to do?