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

please help oh wise monks...

I have a subroutine that writes into the filehandle DATA. I open this for reading and writing like this:

print "output file name: \n"; chomp($outfile=<STDIN>); open (DATA, "+>$outfile" ) || die;
I then call up a subroutine that prints data into DATA. I then want a second subroutine to act on DATA which I call up in subroutine 2 like this:
sub countprog{ my $pos=0; my $printblock=0; while(<DATA>) { #do a load of various things looking line-by-line at my data print DATA2 "results of this 2nd subroutine"; }
I don't think the second subroutine is getting the data properly.

Is there a better way to put data from one subroutine into another ? thanks,

basm100

Replies are listed 'Best First'.
Re: data from one subroutine into a second
by jmcnamara (Monsignor) on Feb 27, 2002 at 15:28 UTC

    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.

      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.

Re (tilly) 1: data from one subroutine into a second
by tilly (Archbishop) on Feb 27, 2002 at 16:31 UTC
    Is there a better way to put data from one subroutine into another ? thanks,

    Yes.

    Have the first subroutine return its data in an array of lines, and then pass that array into the second. like this:

    my @results = second_sub(first_sub(@stuff)); # time passes sub first_sub { my @inputs = @_; my @results; # etc return @results; } sub second_sub { my @results; foreach my $line (@_) { # etc } return @results; }
    In short, rather than writing data to a file, and parsing it in again, just keep it all in native Perl data structures. Simpler, less work, and a better design because you don't parse.
Re: data from one subroutine into a second
by jonjacobmoon (Pilgrim) on Feb 27, 2002 at 16:13 UTC
    I think the conscience here is that we need to see more code, but I agree that it sounds like you are best to build yourself a data structure so you can deal with things in memory instead of on disk. Try it, you'll like it ;)


    I admit it, I am Paco.
Re: data from one subroutine into a second
by dragonchild (Archbishop) on Feb 27, 2002 at 15:34 UTC
    I'm a little confused as to what exactly you're doing.
    • Where does DATA2 come from?
    • Where do you setup DATA as a handle to read from?
    It sounds like you need a better description of what you're doing before you can do it correctly.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.


      Where do you setup DATA as a handle to read from?

      Here: open (DATA, "+>$outfile");

      --
      John.