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

I am a high school student (and a perl newbie) and I want to write a program to download my grades from the sites where they are posted. My goal is to print the overall grade. This is what I have so far (for history):
#!/usr/bin/perl -w use LWP::Simple; use HTML::Obliterate qw(extirpate_html); $url="where my grades are.html"; $mypage=get($url); my $q=extirpate_html( $mypage ); open OUT, ">/home/jesse/Desktop/grades/history.txt"; select OUT; print $q; close OUT; open(GRADE, "/home/jesse/Desktop/grades/history.txt"); $. = 0; do { $history = <GRADE> } until $. == 32 || eof; #32 is the line with + my grade print $history close(GRADE);
This actually works fairly well for english, but when I try it with my history grade it doesn't work at all. If I open history.txt, it shows all the information from the web page. Yet with a simple test in perl...
#!/usr/bin/perl -w use LWP::Simple; use HTML::Obliterate qw(extirpate_html); $url="where my grades are.html"; $mypage=get($url); my $y=extirpate_html( $mypage ); print $y;
...I see that only a garbled bit of the information is really saved as $y. How can I read the whole thing if only part is saved? Is this information not really there?

Replies are listed 'Best First'.
Re: Perl doesn't read whole file
by shmem (Chancellor) on Dec 02, 2007 at 22:59 UTC

    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}
      That won't print the last line, that will always print undef. You want
      while (<GRADE>) { $test = $_; }
      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}
Re: Perl doesn't read whole file
by KurtSchwind (Chaplain) on Dec 02, 2007 at 23:04 UTC

    Could you perhaps post up your history.txt file? and the output from the 2nd part. Does it read to a certain line and then die? Or does it die at random times while reading?

    Also, just a heads up. perl -w and the open are somewhat deprecated. Try use warnings; instead of the -w. And there is a 3 argument open to use instead of the older 2 argument style.

    --
    I used to drive a Heisenbergmobile, but every time I looked at the speedometer, I got lost.
      It's nice to see people willing to help a lost newbie :-) My history.txt file is on my public scratchpad but is ugly and hard to read (I didn't format it). The output is always "at thing line 8.ia MapationslER 6" where the 2nd part is made into a script called thing.
        While this doesn't address the mystery of a truncated file output - try the following with the history.txt file:
        perl -n00 -e 'print $1,"\n" if /Overall Grade.*?([\d.]+%)/ms' history. +txt

        That should produce

        95.9%

        i.e. the contents of line 32.

        See perlrun for command line switches, perlre for regular expressions.

        <update>

        Using a regular expression here is more reliable, since the line number where the sought information is stored arguably is more prone to changes than the format of the retrieved file.

        </update>

        --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}

        Please post the file contents in this thread rather than on your scratch pad. Scratch pad contents are transient whereas thread contents may be useful to other people in the distant future.


        Perl is environmentally friendly - it saves trees