simple question : how to download an image?

S

ST

I am using LWP::simple to download images on windows XP using Perl
5.6.1. Somehow the downloaded image is corrupt; it shows similar size
as of the proper image but when I open it, it's messed up. Here is
what my script looks like:

use LWP::Simple;
$page =get("http://www.../a.jpg");
open(PAGE,">amy1.jpeg");
print PAGE $page;
close PAGE;

Any idea what might be wrong? Should I be using some other LWP option?

Also, what's the "best" way to invoke perl on windows? I use the right
mouse botton on the script file and use "open with -> perl
interpretor..". However, this does not show the errors/warnings. Is
there a debug mode / command line mode available for perl on windows?

Thanks!
 
X

Xaonon

ST said:
I am using LWP::simple to download images on windows XP using Perl
5.6.1. Somehow the downloaded image is corrupt; it shows similar size
as of the proper image but when I open it, it's messed up. Here is
what my script looks like:

use LWP::Simple;
$page =get("http://www.../a.jpg");
open(PAGE,">amy1.jpeg");
print PAGE $page;
close PAGE;

The print function may be trying to turn newline characters into the \r\n
combo that Windoze uses. Try getstore( "http://www.../a.jpg", "amy1.jpeg" )
instead, or use syswrite.
 
B

Ben Morrow

I am using LWP::simple to download images on windows XP using Perl
5.6.1. Somehow the downloaded image is corrupt; it shows similar size
as of the proper image but when I open it, it's messed up. Here is
what my script looks like:

use strict;
use warnings;
use LWP::Simple;
$page =get("http://www.../a.jpg");

my $page = ...;
open(PAGE,">amy1.jpeg");

open my $PAGE, ">amy1.jpeg" or die "can't create amy1.jpeg; $!";
binmode $PAGE; # this is your problem

Without 'binmode' Win32 translates all newlines into CRLF
combinations, which will mess up binary data like images.
print PAGE $page;
close PAGE;

Any idea what might be wrong? Should I be using some other LWP option?

Also, what's the "best" way to invoke perl on windows? I use the right
mouse botton on the script file and use "open with -> perl
interpretor..". However, this does not show the errors/warnings. Is
there a debug mode / command line mode available for perl on
windows?

Make sure c:/perl/bin is in your path, and then type 'perl script.pl'
at a DOS prompt. (If you don't know how to find a dos prompt,
Start/Run/cmd.exe)

Ben
 
P

pkent

I am using LWP::simple to download images on windows XP using Perl
5.6.1. Somehow the downloaded image is corrupt; it shows similar size
as of the proper image but when I open it, it's messed up. Here is
what my script looks like:

use strict;
use warnings;
use LWP::Simple;
$page =get("http://www.../a.jpg");

my $page, and check that it's defined - $page will be undef on error.
open(PAGE,">amy1.jpeg");

open(PAGE, ....) or die "Cannot open file: $!";
binmode(PAGE);
print PAGE $page;
close PAGE;

Also, what's the "best" way to invoke perl on windows? I use the right
mouse botton on the script file and use "open with -> perl
interpretor..". However, this does not show the errors/warnings. Is
there a debug mode / command line mode available for perl on windows?

If it's installed the normal way you should be able to type 'perl
foo.pl' in a command prompt if you're in the right directory. You should
also be able to type 'foo' (again if you're in the same directory) if a
certain environment variable contains the .pl suffix... I think it's
PATHEXT... and there is the right association between .pl and the perl
interpreter binary. I would imagine that ActivePerl sets all this up
automatically for you.

P
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,824
Members
47,371
Latest member
Brkaa

Latest Threads

Top