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

I put some information in a temp file but when I try to print what’s in the file, I get this instead--> IO::File=GLOB(0x8791…). What does this mean? Is the data not being written into the temp file? Heres some of my code.
foreach $line (@data) { chomp $line; ($timestamp, $dump1, $dump2, $out, $dump3, $dump4, $dump5, $in, $dump6 +)=split(/\t/, $line); # Sort out needed columns foreach ($line) { # Format time/date my $datetime = DateTime::Format::Excel->parse_datetime($timestamp); $timestamp = $datetime->ymd . $datetime->hms; seek ($fh, 0, 0) or die "Seek: $!"; print $fh "$timestamp $out $in\t"; } print $fh; } close (FILE);

Replies are listed 'Best First'.
Re: what does IO::File=GLOB mean?
by pc88mxer (Vicar) on Jul 25, 2008 at 19:57 UTC
    You are printing the file handle reference. It is coming from this line:
    print $fh;
    In order to have perl treat $fh as a file handle, you need to use one of the following:
    print {$fh} $_; print $fh $_;
Re: what does IO::File=GLOB mean?
by pjotrik (Friar) on Jul 25, 2008 at 20:14 UTC
    This code looks indeed broken. What are you trying to do?

    - The foreach ($line) is redundant.
    - You're rewriting the file from the beginning for every line thanks to that seek. That's most probably not what you want.
    - To print the contents of the file, you have to have the file opened for reading and read from it. print $fh; doesn't print the file, it prints the contents of variable $fh, which happens to be a filehandle.

      I hope this helps explain what I am trying to do...

      My first foreach ($line) collects a row of data from a file. The second foreach ($line) reformats the first "cell" (for lack of a better word) into a new date/time format.


      By using the seek function I was attempting to append to the end of the file instead of overwriting it. What is the correct way to do this?


      Included in my code I have this line:
      $fh = IO::File->new_tmpfile or die "Unable to make new temp file: $!";
      Does this put the contents of the file in $fh or is it just pointing to the file? Because this is a randomly generated file, I want to be able to access the file using a variable since I don't know the name of the temp file. How can I do this and be able to display what is in the file?
        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.

        The call to seek() with those parameters is not doing what you want, then. See seek (or try perldoc -f seek on your system) to see what the arguments for seek() are.