Subject: | Allow ->bind and ->connect with named arguments |
There's a disparity in convenience between
use IO::Socket::Packet;
my $sock = IO::Socket::Packet->new( IfIndex => 5 );
and
use IO::Socket::Packet;
use Socket::Packet qw( pack_sockaddr_ll ETH_P_ANY );
my $sock = IO::Socket::Packet->new;
$sock->bind( pack_sockaddr_ll( ETH_P_ANY, 5, 0, 0, '' ) );
It would be nice if IO::Socket->bind (and ditto ->connect) could take a
collection of named arguments instead of a single packed address.
use IO::Socket::Packet;
my $sock = IO::Socket::Packet->new;
$sock->bind( IfIndex => 5 );
I observe that right now it's always an error to pass more than one
argument to ->bind:
sub bind {
@_ == 2 or croak 'usage: $sock->bind(NAME)';
...
This surely gives way to a really neat implementation:
sub bind {
my $sock = shift;
my $addr = @_ > 1 ? $sock->pack_bindaddr( @_ ) : shift;
return bind($sock, $addr) ? $sock
: undef;
}
Then any subclass that wishes to provide a neater implementation of bind
args, can provide such a method; e.g.
sub pack_bindaddr
{
shift;
my %args = @_;
pack_sockaddr_ll( $args{Protocol} || ETH_P_ANY, $args{IfIndex} || 0,
0, 0, '' );
}
(with analogous implementation for ->connect)
Such code could probably be largely lifted from each subclass's
->configure method anyway.
This should be easy to implement, as it only adds new behavior which
right now would be an error; so there ought not be any risk of
clobbering existing code. Subclasses can implement these packing methods
piecemeal; we don't have to update every subclass all at once.
If you'd be happy with such an idea, I'd be quite happy to go about
actually writing code, tests, updating docs, etc...
--
Paul Evans