mscharrer has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm writing an OO module which new method can be given a file handle. Internally I like to use IO::File or IO::Handle (I only need read,print,open,close, maybe write), but I like also to support normal Perl file handles.

Now I wrote some code and tests and figured out that normal file handles seems to be already IO::Handle objects, even without 'use IO::Handle'. I'm using Perl 5.8.8. Can anyone tell me when this feature got included into Perl so I know how downward compatible my code will be?

My code should work with *HANDLE, \*HANDLE and *HANDLE{IO} and of course with IO::Handle and derived objects.

use Test::More tests => 4; use IO::Handle; use strict; use warnings; sub ishandle { my $h = shift; return $h if eval { $h->isa('IO::Handle') }; return *$h{IO} if ref $h eq 'GLOB' or ref \$h eq 'GLOB'; return $h; } isa_ok( ishandle( new IO::Handle ), 'IO::Handle' ); isa_ok( ishandle( *STDIN ), 'IO::Handle' ); isa_ok( ishandle( \*STDIN ), 'IO::Handle' ); isa_ok( ishandle( *STDIN{IO} ), 'IO::Handle' ); __END__ 1..4 ok 1 - The object isa IO::Handle ok 2 - The object isa IO::Handle ok 3 - The object isa IO::Handle ok 4 - The object isa IO::Handle
The function returns a IO::Handle object from all valid inputs. Any comments? Does anyone know a better way to do this?

Support for literal names like 'STDIN' could be added with:

no strict 'refs' return *$h{IO} if eval { *$h{IO} };

Replies are listed 'Best First'.
Re: Normal handles as IO::Handle
by Anonymous Monk on Apr 18, 2008 at 10:41 UTC
    D:\>grep IO::Handle D:\perl\lib\pod\perl*del* D:\perl\lib\pod\perl5004delta.pod:File handles are now stored internal +ly as type IO::Handle. The D:\perl\lib\pod\perl5004delta.pod:IO::Handle, IO::Seekable, and IO::Fi +le. We suggest, but do not D:\perl\lib\pod\perl5004delta.pod: IO/Handle.pm IO::Handle +extension Perl module D:\perl\lib\pod\perl5004delta.pod: IO::Handle D:\>corelist IO::Handle IO::Handle was first released with perl 5.00307
      Thanks for the information and to show me how to get it.
Re: Normal handles as IO::Handle
by ikegami (Patriarch) on Apr 18, 2008 at 10:47 UTC

    Can anyone tell me when this feature got included into Perl so I know how downward compatible my code will be?

    Since Perl 5.4 (1997)

    Now I wrote some code and tests and figured out that normal file handles seems to be already IO::Handle objects, even without 'use IO::Handle'.

    Yes, but you do need to use IO::Handle; to load the methods if you want to use them.

    >perl -e"STDOUT->print('');" Can't locate object method "print" via package "IO::Handle" at -e line + 1.
      I knew that. My idea is that I have use IO::Handle in my module and the user scripts can just pass *STDIN or any other handle without knowing about IO::Handle.
Re: Normal handles as IO::Handle
by ikegami (Patriarch) on Apr 18, 2008 at 20:53 UTC
    You don't even need to convert the argument into a reference.
    >perl -MIO::Handle -le"( STDOUT )->print('foo');" foo >perl -MIO::Handle -le"( *STDOUT )->print('foo');" foo >perl -MIO::Handle -le"( *STDOUT{IO} )->print('foo');" foo >perl -MIO::Handle -le"( \*STDOUT )->print('foo');" foo >perl -MIO::Handle -le"( 'STDOUT' )->print('foo');" foo