in reply to data from one subroutine into a second


One thing that may be missing is that after writing to DATA you have to go back to the beginning of the file before you can start reading from it. Insert this in your second sub: seek(DATA, 0, 0); However, this may be overly complicated. Perhaps you could just store the data in an array and pass that around instead of writing to a file.

--
John.

Replies are listed 'Best First'.
(Ovid) Re(2): data from one subroutine into a second
by Ovid (Cardinal) on Feb 27, 2002 at 15:46 UTC

    You'll be in for a nasty surprise if you try and do that :) Try this snippet:

    use strict; seek DATA, 0, 0; print while <DATA>; __DATA__ This is a test

    From playing with that, you'll see that the seek returns to the beginning of the file, not the beginning of DATA. To correct that, you'll want to tell where DATA starts:

    use strict; my $start = tell DATA; seek DATA, $start, 0; print while <DATA>; __DATA__ This is a test

    Update: Reading jmcnamara's response. Boy, do I feel stupid. However, I'm now tempted to launch into my don't name a filehandle DATA rant, since this is a source of confusion (as demonstrated above). I recall ranting about this before. Humph. Maybe I'm just embarrassed :)

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.


      You'll be in for a nasty surprise if you try and do that :)

      He isn't reading from __DATA__ he is reading from a filehandle called DATA that he opened himself.

      In which case there is no problem:

      #!/usr/bin/perl -w use strict; open DATA, "+>output.txt" or die "Error message here: $!\n"; print DATA "hello, world\n"; seek(DATA, 0, 0); while (<DATA>) { print $_; } close DATA;

      --
      John.