Why is loading my image using Perl LWP giving me the wrong file size?

I am trying to get an image from an HTTP server using Perl.

I have the full file url and I'm trying to use

my $data = LWP::Simple::get $params{URL}; my $filename = "image.jpg"; open (FH, ">$filename"); print FH $data; close (FH); 

Now, logically, at least it should work for me. But the files are a little different sizes, and I can not understand why.

Help!

+7
perl image lwp
source share
2 answers

You need to use binmode to write image data to disk correctly.

 my $data = LWP::Simple::get $params{URL}; my $filename = "image.jpg"; open (FH, ">$filename"); binmode (FH); print FH $data; close (FH); 

Otherwise, it is interpreted as text, and newlines are lost.

+14
source share

Dave is right, you must / must set the file descriptor to binary mode. But you could do it all in one go:

 LWP::Simple::getstore( $params{URL}, 'image.jpg' ); 
+13
source share

All Articles