Using Windows ActiveState Perl 5.8.8 build 822.

I'm extremely confused.

I just tossed together a simple script that would scan the Wizards Gatherer web page and spit out a card's ID and name, separated by a semicolon.

Now I'm a little out of practice, but I managed to get it to work first try, printing to STDOUT. I then said to myself "okay, why bother with the file operations, let the shell do it."

perl GatherScan.pl > carddb.txt

I come back in a few minutes to find no data in carddb.txt. It's reporting a filesize of 0.

I reworked it using IO::File to do the file writes itself. Thus adding

use IO::File; my $file = IO::File->new('>carddb.txt') or die $!;
to the front of my file and changing my print invocations to $file->print.

That done, I ran it again, without the redirect, and found, again, no data in the file. I tried changing the file name to no avail. I added prints to STDERR to verify the data was being retrieved/generated, and I got exactly what I wanted out of it, but no actual data in the file.

Finally, I tried redirecting STDERR to the file and swapping everything (by this time I'd given up on having perl write to the file directly.) Suddenly, the file has data!

Here's the program I ended up with, with the swap completed:

use LWP::Simple 'get'; use strict; use warnings; my $basePath = "http://ww2.wizards.com/gatherer/CardDetails.aspx?id="; for(my $i = 1; $i < 129700; ++$i){ print STDOUT "Getting $i ... "; my $content = get($basePath.$i); print STDOUT "Done\n"; if($content){ $content =~ m/<span id="_lblCardTitle">((?:\w|\s)+)<\/span>/; my $result = $i.';'.$1."\n"; print STDERR $result; print STDOUT $result; }else{ print STDOUT 'Failed on ', $i, "\n"; } sleep 1; } print STDOUT "Done!\n";

As you can see, it was meant to be a simple little script, doesn't even do proper HTML parsing (I know).

So, the question is, does anyone know why I couldn't get data from STDOUT to a file.. or into a file opened in perl?

Thanks.


0x596F752068617665206265656E2068657865642E


In reply to Can't write files (even redirected from shell) by Flame

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.