Here is a more robust patch:

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")

In reply to (tye)Re: Unconfuse filehandles and classes by tye
in thread Unconfuse filehandles and classes by tilly

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.