Read your update. If all what you want is to create socket once, and use it repeatedly, there is certainly no need for you to convert to IO::Socket. Just like you can use variables when you open files, you can do the same thing towards socket, and thus it follows the usual scope rules. For example: (code is tested)
use Socket; use strict; use warnings; my $yahoo; socket($yahoo, PF_INET, SOCK_STREAM, getprotobyname('tcp')) || die "so +cket: $!"; connect($yahoo, sockaddr_in(80, inet_aton("www.yahoo.com"))) || die "c +onnect: $!"; syswrite($yahoo, "GET / HTTP/1.1\r\nHost: www.yahoo.com\r\n\r\n"); read_one_line($yahoo); read_one_line($yahoo); sub read_one_line { my $sock = shift; my $line = <$sock>; print $line; }
This also works, but I like the first solution better:
use Socket; use strict; use warnings; socket(YAHOO, PF_INET, SOCK_STREAM, getprotobyname('tcp')) || die "soc +ket: $!"; connect(YAHOO, sockaddr_in(80, inet_aton("www.yahoo.com"))) || die "co +nnect: $!"; syswrite(YAHOO, "GET / HTTP/1.1\r\nHost: www.yahoo.com\r\n\r\n"); read_one_line(*YAHOO); read_one_line(*YAHOO); sub read_one_line { my $sock = shift; my $line = <$sock>; print $line; }
In reply to Re: Translating Socket to IO::Socket;
by pg
in thread Translating Socket to IO::Socket;
by BUU
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |