CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Problem passing telnet connection to function

 



DanB
New User

Feb 25, 2013, 1:07 AM

Post #1 of 3 (403 views)
Problem passing telnet connection to function Can't Post

Hi to everyone and pleased to meet you all!

I have a problem passing a telnet connection to sub.

I don't have any problem if I create the connection and then use it, like this


Code
my $conn = Net::Telnet->new(Fhopen => $socket); 
$conn->print($command.(chr 13));
$conn->recv($data, 1024);
# OK!


but if I try to pass the connection to a subroutine like this


Code
 
my $conn = GetConnection(...);
RunCommand($conn, $command);

sub GetConnection
{
...
my $conn = Net::Telnet->new(Fhopen => $socket);
return $conn;
}

sub RunCommand
{
my $conn = shift;
my $command = shift;
$conn->print($command.(chr 13));
$conn->recv($data, 1024);

# NOTHING!!!!
}


it doesn't work: don't do anything.

Any ideas?

Thank you for support


(This post was edited by DanB on Feb 25, 2013, 1:12 AM)


BillKSmith
Veteran

Feb 25, 2013, 7:47 AM

Post #2 of 3 (382 views)
Re: [DanB] Problem passing telnet connection to function [In reply to] Can't Post

Are you using strict and warnings? I do not see anything wrong with your code if $socket is in scope. Even if it is, it would be better style to pass it to GetConnection as an argument.

I would prefer that you use "\r" rather than chr 13. It would be much easier for readers of your code to recognize the purpose.
Good Luck,
Bill


Kenosis
User

Feb 25, 2013, 10:34 AM

Post #3 of 3 (373 views)
Re: [DanB] Problem passing telnet connection to function [In reply to] Can't Post

Have you tried passing a reference to the connection to the subroutine?


Code
use strict; 
use warnings;

...

RunCommand( \$conn, $command );

...

sub RunCommand {
my $conn_ref = shift;
my $command = shift;

$$conn_ref->print( $command . ( chr 13 ) );
$$conn_ref->recv( $data, 1024 );

}


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives