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

Good Day Monks!

Wondering if someone could help me address a little problem. I have a sample bit of code below that is not acting like I expect it to. Basicly I want to make a string into a filehandle to pass to another module. My problem is that the file handle can be written to, but stays undef and can not be read. I have tried this with both IO::Sting and IO::Scalar with the same results.

ActiveState Build 860
IO::String Ver. 2.01
IO::Stringy Ver. 2.104

use strict; use warnings; use IO::String; my $ioData; my $io = IO::String->new($ioData); # Load the Sting filehandle $io print $io "This is a test\n"; print $io "If you can see this it works!"; # At this point $ioData contains the text above # as expected. However, $io is still undefined. while ( <$io> ) { print $_; # This print is skipped since # $io is undefined. I do not think it has anything to do # with the way I am calling <$io> since from the # debugger, $io is never defined. }
Thanks!

Replies are listed 'Best First'.
Re: IO::String Defining FH for writing, but not reading.
by Zaxo (Archbishop) on Oct 09, 2003 at 19:53 UTC

    Where you try to read the $io handle, you are positioned at the end of the 'file'. Just before you start reading, say,

    seek $io, 0, 0; while (<$io>) {
    That will fix it.

    After Compline,
    Zaxo

Re: IO::String Defining FH for writing, but not reading.
by Anonymous Monk on Oct 09, 2003 at 19:46 UTC
    <code> seek($io, 0, 0); while (<$io>) { print $_; }
Re: IO::String Defining FH for writing, but not reading.
by Anonymous Monk on Oct 09, 2003 at 23:11 UTC
    Duh!

    It's always the little stuff that gets you!

    Thank you all!!