in reply to Perl doesn't read whole file

update: *Please* don't edit your original post deleting parts: if you do, answers are out of context and just look like being delirated out of the blue. To delete something, use <strike> tags, mark additions with a prominent "update". Thanks.

I can't think of why (in normal circumstances) perl would read only part of a file.

Perhaps the file is UTF8 encoded, while your terminal is set to latin1? I have seen terminals been blown up by UTF8, and after the shell gained control, the terminal settings being restored and the prompt returning as if nothing happened.

Do you see the last line of the file in question with the following?

#!/usr/bin/perl -w open(GRADE, "/home/jesse/Desktop/grades/history.txt"); while ($test = <GRADE>) { ; # do nothing } print $test; # print last line close GRADE;

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Replies are listed 'Best First'.
Re^2: Perl doesn't read whole file
by ikegami (Patriarch) on Dec 03, 2007 at 00:42 UTC
    That won't print the last line, that will always print undef. You want
    while (<GRADE>) { $test = $_; }
Re^2: Perl doesn't read whole file
by colonelcrayon (Novice) on Dec 02, 2007 at 23:28 UTC
    Thanks for responding. The file is UTF8 encoded, but I'm not sure what my terminal is set to. However, everything works for english (which is also UTF8). Your script just returns "Use of uninitialized value in print at thing line 8, <GRADE> line 1."
      Your script just returns "Use of uninitialized value in print at thing line 8, <GRADE> line 1."

      which probably means the file is zero bytes long (i.e. empty) <update> but more likely it means I've made a silly mistake. See ikegami's note below. </update>

      update: Oh, and another point: always check the return value of functions; the script is more aptly written as

      #!/usr/bin/perl -w open(GRADE, "/home/jesse/Desktop/grades/history.txt") or die "Can't open '/home/jesse/Desktop/grades/history.txt': $!\n" +; while ($test = <GRADE>) { ; # do nothing } print $test; # print last line close GRADE;

      --shmem

      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
        You raise a good point about the return value of functions - I was just lazy. The file is not empty. If I look at its properties, I see that it has 2,638 Bytes. Using the shell, I tried cat /home/jesse/Desktop/grades/history.txt. It's result was "ationslER 6", part of the fragment my script produces.