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

Howdy gents,

I used to be able to code fluently, but after a year in the military (so far) and recently only being able to code on windows machines, I'm left confused about many things like when I first started. Don't think that languages aren't perishible knowledge.

Anyhow, here's my code, it just grabs a image every few seconds and -tries- to write it to disk. The problem is that it doesn't write the image correctly. I compared the files and the differences are sooo tiny (one or two bytes difference) it looks like the file gets corrupted. No idea.

Any help, appreciated much! Again this is on win xp with uhh Activestate ActivePerl 5.8.0.

require HTTP::Request; require HTTP::Response; require LWP::UserAgent; while( 1 ) { $ua = LWP::UserAgent->new; $request = HTTP::Request->new( GET => "$url" ); $response = $ua->request( $request ); if ( $response->is_success ) { ( $sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst ) = localtime( time ); my $content_dump = ( ($year += 1900) . ".$mon.$hour.$min.$sec.jpg" + ); open( PICDUMP, ">$outputbase/$content_dump" ) || die( "Couldn't open $outputbase/$content_dump ($!)" ); print( PICDUMP $response->content ) || die( "Couldn't print to file ($!)" ); print( STDOUT "Wrote $content_dump\n" ); close( PICDUMP ); sleep 4; } else { print $response->error_as_HTML; } }

Replies are listed 'Best First'.
Re: grabbing a image with HTTP module
by pfaut (Priest) on Jan 28, 2003 at 00:40 UTC

    Since you said you're on Windows, you probably want to use binmode (perldoc -f binmode) on the local file before writing to it.

    --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
      Thank you. I grew up on Unix so I didn't even know there was a perlport,.. everything just worked ;)
Re: grabbing a image with HTTP module
by silent11 (Vicar) on Jan 28, 2003 at 00:40 UTC
    when writing binary files on windows don't forget.
    binmode(FILEHANDLE);


    -Silent11
Re: grabbing a image with HTTP module
by PodMaster (Abbot) on Jan 28, 2003 at 00:41 UTC
    Read `perldoc -f binmode' and `perldoc perlport' and it should clear things up.


    MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
    ** The Third rule of perl club is a statement of fact: pod is sexy.