mscharrer has asked for the wisdom of the Perl Monks concerning the following question:
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.
The function returns a IO::Handle object from all valid inputs. Any comments? Does anyone know a better way to do this?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
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 | |
by mscharrer (Hermit) on Apr 18, 2008 at 10:47 UTC | |
|
Re: Normal handles as IO::Handle
by ikegami (Patriarch) on Apr 18, 2008 at 10:47 UTC | |
by mscharrer (Hermit) on Apr 18, 2008 at 10:55 UTC | |
|
Re: Normal handles as IO::Handle
by ikegami (Patriarch) on Apr 18, 2008 at 20:53 UTC |