I am trying to test a file handle that may be created from a file or from a 'piped open'. Now you cant seek on a file handle from piped open - its a stream, you get characters from the pipe, you cant jump 'backwards' or 'forwards'.

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

Code
#!/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;


In reply to $io->can('seek') == true, but $io->seek(pos, whence) == 'Illegal seek' - bug ? by leriksen

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.