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. # # Only class methods are subject to this problem. This version # of IO::patch even handles class methods that are AUTOLOADed, # but doesn't trap class methods that have the same name as # methods (or routines) in IO::Handle except for new(). require IO::Handle; use vars qw( $AUTOLOAD ); my $oldNew= \&IO::Handle::new; *IO::Handle::new= \&newNew; *IO::Handle::AUTOLOAD= \&AUTOLOAD; sub newNew { my $pkg= $_[0]; if( UNIVERSAL::isa( $pkg, "GLOB" ) ) { my $name= *{$pkg}{NAME}; my $can= UNIVERSAL::can( $name, "new" ); require Carp if $^W; if( $can ) { Carp::carp( "File handle vs. package name conflict ($name)" ) if $^W; $_[0]= $name; goto &$can; } Carp::carp( qq(Called new() on an open IO::Handle or can't locate) . qq( object method "new" via package "$name") ) if $^W; } goto &$oldNew; } sub AUTOLOAD { my $pkg= $_[0]; my $meth= $AUTOLOAD; require Carp; if( UNIVERSAL::isa( $pkg, "GLOB" ) ) { my $name= *{$pkg}{NAME}; $meth =~ s/^IO::Handle:://; my $can= UNIVERSAL::can( $name, $meth ); if( $can ) { Carp::carp( "File handle vs. package name conflict ($name)" ) if $^W; $_[0]= $name; goto &$can; } $can= UNIVERSAL::can( $name, "AUTOLOAD" ); if( $can ) { eval { require Devel::Peek } or Carp::croak( qq(Can't AUTOLOAD method "$meth" via package "$name") . qq( unless you install Devel::Peek\n) . $@ ); my $gv= Devel::Peek::CvGV( $can ); { my $alPkg= *{$gv}{PACKAGE}; no strict 'refs'; ${$alPkg."::AUTOLOAD"}= $name . "::" . $meth; } Carp::carp( "File handle vs. package name conflict ($name)" ) if $^W; $_[0]= $name; goto &$can; } Carp::croak( qq(Can't locate object method "$meth" via package "$name") ); } Carp::croak( qq(Can't locate object method "$meth" via package "IO::Handle") ); }