I'm not sure why you're using a file for this. I would store the values in an array, if there's not a huge amount of them (which is not the case, since you already have the whole @data in memory). But perhaps there's a reason, not shown in the snippet you've posted.

Your first foreach $line (@data) processes the data line by line. Your foreach ($line) is nested inside the first foreach and iterates through the list consisting of one item: ($line). I.e. it has no effect - it is the same as if the inner loop wasn't there.

Your seek is again inside the foreach $line (@data), therefore you move your position to the beginning of the file before each write. new_tmpfile opens a new file for read/write. The position is already on the start of the file, you don't have to seek before writing.

When you're done writing and want to read from the file, you'll have to seek to the beginning of the file (your arguments to seek are fine for this purpose). Then you may start writing like from any other filehandle (while (<$fh>) {... or simply print <$fh>;). Don't forget to flush your file when you're finished writing.

And another remark, you don't have to define variables for parts of the split you don't need, you may just call ($timestamp, undef, undef, $out,...) = split(/\t/, $line);

HTH

UPDATE: Here's a code sample. Looks like the file is flushed because of the seeking, so it's not necessary to call flush.

#!/usr/bin/perl use warnings; use strict; use IO::File; my $fh = IO::File->new_tmpfile or die "Unable to make new temp file: $ +!"; my @data = <DATA>; foreach my $line (@data) { chomp $line; my ($first, undef, undef, $last) = split(/\t/, $line); print $fh "$first $last\n"; } #$fh->flush(); $fh->seek(0, 0) or die "Seek: $!"; print <$fh>; $fh->close(); __DATA__ 1 2 3 4 5 6 7 8 9 0 1 2

Another UPDATE: Changed the seek to a more object-ish notation, simplified the print.


In reply to Re^3: what does IO::File=GLOB mean? by pjotrik
in thread what does IO::File=GLOB mean? by ITmajor

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.