On Mon Jan 11 15:37:20 2010, gbarr@pobox.com wrote:
Show quoted text> Any subclass of IO::Socket should define its own configure, otherwise
> you will end up
> with a loop due to the line that follows the bless
>
> bless($sock, $domain2pkg[$domain]);
> $sock->configure($arg);
...
Show quoted text> IO::Socket::Netlink needs to define a configure method, even if it
> does nothing
It does contain one of these. That doesn't help. See below.
Show quoted text> IO::Socket::configure was added to allow
>
> IO::Socket->new(Domain => $domain);
>
> to work by re-blessing into the package for the socket domain
That's fine. That's an up-cast; a rebless into a -more- specific class.
The problematic blessing that's going on here is a down-cast; a rebless
into a -less- specific class, one which loses information.
Given an IO::Socket::Netlink which has been mistakenly reblessed by
IO::Socket::configure, it can work out which actual subclass it should
be, by inspecting the Protocol field (analogous to what IO::Socket
itself is doing with the domain).
But that doesn't help ultimately. Most Netlink sockets will in fact be
Netlink::Generic, or rather subclasses of them.
There is no fact about a Netlink::Generic object which it can inspect to
see which way to up-cast itself. E.g. a ::Taskstats socket just happens
to have certain messages passed into the kernel, and expect certain
replies or broadcasts.
It really would be best not to break the object's own identity like this.
Can I perhaps instead suggest replacing:
bless($sock, $domain2pkg[$domain]);
$sock->configure($arg);
With something like:
my $pkg = $domain2pkg[$domain];
if( $sock->isa( $pkg ) ) {
$pkg->can( "configure" )->( $sock, $arg );
}
else {
bless($sock, $pkg);
$sock->configure($arg);
}
This will not alter the up-cast case for normal use in e.g.
IO::Socket->new( Domain => AF_INET ); but it will then mean that when
specific subclass constructors have already been called, the sockets
they construct won't lose their identity in a way that cannot be
reconstructed.
--
Paul Evans