Esteemed Monks,

I'm having a bit of troubles with in-memory filehandles, which seem to change shape behind the scenes, as the following example code seems to demonstrate.

#!/usr/bin/perl use strict; use warnings; use IO::Handle; my $data = 'some sample data here'; open my $fh, '<', \$data or die "open(): $!"; print {*STDERR} 'ref $fh yields ', ref $fh, "\n"; print_prop('$fh->can("opened")', $fh->can('opened')); print_prop('$fh->can("seek")', $fh->can('opened')); print_prop('$fh->can("seekable")', $fh->can('opened')); print_prop('$fh is IO::Handle', $fh->isa('IO::Handle')); print_prop('$fh->can("opened")', $fh->can("opened")); print_prop('$fh->opened()', $fh->opened()); print_prop('$fh->can("seek")', $fh->can("seek")); eval {print_prop('$fh->seek(0, 0)', $fh->seek(0, 0));} or print "$@"; print_prop('$fh is tied', tied $fh); close $fh; sub print_prop { my ($msg, $flag) = @_; print {*STDERR} $msg, '? ', ($flag ? 'yes' : 'no'), "\n"; return; } __END__ ref $fh yields GLOB $fh->can("opened")? no $fh->can("seek")? no $fh->can("seekable")? no $fh is IO::Handle? no $fh->can("opened")? no $fh->opened()? yes $fh->can("seek")? no Can't locate object method "seek" via package "IO::Handle" at memhandl +e.pl line 19. $fh is tied? no
Good monk dada suggested that there is a fallback to IO::Handle somewhere, and this makes sense from what I see. Now I would like to understand:
  1. is this theory correct?
  2. is IO::Handle the only possible fallback?
  3. why the fallback class doesn't support seek() and tell()? I'm able to use seek() and tell() functions on them! (e.g. seek($fh, 0, SEEK_SET); works)
Any light is highly appreciated :)

Flavio
perl -ple'$_=reverse' <<<ti.xittelop@oivalf

Don't fool yourself.

In reply to In memory filehandles by polettix

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.