Flame has asked for the wisdom of the Perl Monks concerning the following question:
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.txtI 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
to the front of my file and changing my print invocations to $file->print.use IO::File; my $file = IO::File->new('>carddb.txt') or die $!;
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Can't write files (even redirected from shell)
by ikegami (Patriarch) on Jan 14, 2008 at 04:13 UTC | |
by Flame (Deacon) on Jan 14, 2008 at 04:17 UTC | |
by ikegami (Patriarch) on Jan 14, 2008 at 04:21 UTC | |
by Flame (Deacon) on Jan 14, 2008 at 04:27 UTC | |
by halley (Prior) on Jan 14, 2008 at 20:20 UTC | |
by ikegami (Patriarch) on Jan 15, 2008 at 00:49 UTC | |
|
Re: Can't write files (even redirected from shell)
by davidrw (Prior) on Jan 14, 2008 at 04:19 UTC | |
by Flame (Deacon) on Jan 14, 2008 at 04:23 UTC |