leriksen has asked for the wisdom of the Perl Monks concerning the following question:
I thought I could test what kind of hadle I had by checking inheritance from IO::Seekable, or if the handle supported the 'seek' method - a file should, but a stream shouldn't.
But that doesn't seem to be the case - handles from 'piped open' say they inherit from IO::Seekable, and that they can('seek'), but if you do seek(), they fail.
Is that a bug ? Code and output follows
#!/usr/bin/perl -w use strict; use IO::File; my $plain = IO::File->new('WEEK3.csv', O_RDONLY) or die "cannot open plain :: $!"; my $zipped = IO::File->new('unzip -qq -p WEEK3.zip |') or die "cannot open zipped :: $!"; print "plain IO::File\n" if $plain->isa('IO::File'); print "plain IO::Handle\n" if $plain->isa('IO::Handle'); print "plain IO::Seekable\n" if $plain->isa('IO::Seekable'); print "plain can seek\n" if $plain->can('seek'); print "zipped IO::File\n" if $zipped->isa('IO::File'); print "zipped IO::Handle\n" if $zipped->isa('IO::Handle'); print "zipped IO::Seekable\n" if $zipped->isa('IO::Seekable'); print "zipped can seek\n" if $zipped->can('seek'); use Fcntl qw(SEEK_END); if ($zipped->isa('IO::Seekable')) { print "seeking\n"; $! = 0; my $rc = $zipped->seek(0, SEEK_END); print "rc == $rc :: $!\n"; } $! = 0; if ($plain->isa('IO::Seekable')) { print "seeking\n"; <$plain>; my $rc = $plain->seek(0, SEEK_END); print "rc == $rc :: $!\n"; }
Output
plain IO::File plain IO::Handle plain IO::Seekable plain can seek zipped IO::File zipped IO::Handle zipped IO::Seekable zipped can seek seeking rc == :: Illegal seek seeking rc == 1 ::
use brain;
|
|---|