From: Gisle Aas Date: Thu, 28 Aug 1997 02:36:50 +0000 (+1200) Subject: IO::Socket autoflush by default, assume tcp and PeerAddr X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d103aadb464fdc9458aeaedf1165336dcbe99ff0;p=p5sagit%2Fp5-mst-13.2.git IO::Socket autoflush by default, assume tcp and PeerAddr Subject: IO::Socket suggestion The following patch to IO::Socket have this effect: - put IO::Socket objects in autoflush mode initially - assume Proto => "tcp" if no Proto is given (IO::Socket::INET) - a single argument to IO::Socket::INET->new is assumed to be a PeerAddr specification. Comments? It allows the following (rather long) one-liner to work: $ perl -I. -MIO::Socket -e '$s=IO::Socket::INET->new("www.perl.com:80");$s->print("HEAD / HTTP/1.0\n\n"); print<$s>' Credited: Andy Dougherty Credited: M.J.T. Guy p5p-msgid: hvi07zvo9.fsf@bergen.sn.no --- diff --git a/ext/IO/lib/IO/Socket.pm b/ext/IO/lib/IO/Socket.pm index ab19170..52c227a 100644 --- a/ext/IO/lib/IO/Socket.pm +++ b/ext/IO/lib/IO/Socket.pm @@ -39,6 +39,8 @@ C only looks for one key C which tells new which domain the socket will be in. All other arguments will be passed to the configuration method of the package for that domain, See below. +Cs will be in autoflush mode after creation. + =back =head1 METHODS @@ -118,7 +120,7 @@ use Exporter; @ISA = qw(IO::Handle); -$VERSION = "1.1602"; +$VERSION = "1.1603"; sub import { my $pkg = shift; @@ -129,6 +131,7 @@ sub import { sub new { my($class,%arg) = @_; my $fh = $class->SUPER::new(); + $fh->autoflush; ${*$fh}{'io_socket_timeout'} = delete $arg{Timeout}; @@ -392,7 +395,7 @@ and some related methods. The constructor can take the following options PeerPort Remote port or service [()] | LocalAddr Local host bind address hostname[:port] LocalPort Local host bind port [()] | - Proto Protocol name "tcp" | "udp" | ... + Proto Protocol name (or number) "tcp" | "udp" | ... Type Socket type SOCK_STREAM | SOCK_DGRAM | ... Listen Queue size for listen Reuse Set SO_REUSEADDR before binding @@ -410,10 +413,13 @@ parenthesis which is used if the service is not known by the system. The C specification can also be embedded in the C by preceding it with a ":". -Only one of C or C needs to be specified, one will be -assumed from the other. If you specify a symbolic C port, -then the constructor will try to derive C and C from -the service name. +If C is not given and you specify a symbolic C port, +then the constructor will try to derive C from the service +name. As a last resort C "tcp" is assumed. The C +parameter will be deduced from C if not specified. + +If the constructor is only passed a single argument, it is assumed to +be a C specification. Examples: @@ -428,6 +434,9 @@ Examples: LocalPort => 9000, Proto => 'tcp'); + $sock = IO::Socket::INET->new('127.0.0.1:25'); + + =head2 METHODS =over 4 @@ -463,6 +472,13 @@ peer host in a text form xx.xx.xx.xx =cut +sub new +{ + my $class = shift; + unshift(@_, "PeerAddr") if @_ == 1; + return $class->SUPER::new(@_); +} + sub _sock_info { my($addr,$port,$proto) = @_; my @proto = (); @@ -535,6 +551,7 @@ sub configure { unless(defined $raddr); } + $proto ||= (getprotobyname "tcp")[2]; return _error($fh,'Cannot determine protocol') unless($proto);