package IO::patch;
# The following code:
# use CGI; my $q= CGI->new(); open CGI, "< $0";
# gives:
# Can't locate object method "new" via package "IO::Handle"
#
# But download this code and put it into lib/IO/patch.pm and
# either add "use IO::patch;" to your script or add
# PERL5OPT=-MIO::patch to your environment, and this problem
# goes away.
require IO::Handle;
my $oldNew= \&IO::Handle::new;
*IO::Handle::new= \&newNew;
sub newNew {
my $pkg= $_[0];
if( UNIVERSAL::isa( $pkg, "GLOB" ) ) {
my $name= *{$pkg}{NAME};
my $can= UNIVERSAL::can( $name, "new" );
if( $can ) {
$_[0]= $name;
goto &$can;
}
require Carp;
Carp::cluck( "Called new() on an open IO::Handle?" )
if $^W;
}
goto &$oldNew;
}
I've tested this quite a bit and it seems to work well.
This suggests an update to IO/Handle.pm if Perl itself
can't be easily patched...
Update: tilly noted that there could be conflicts
for methods other than new(). This problem doesn't happen
for object methods and many modules only have a single
class methods, new(), so I'll be adding an AUTOLOAD but
the above should still be quite useful in the mean time.
-
tye
(but my friends call me "Tye") |