Perl LWP UserAgent Example: Get Picture Using LWP

in Perl

Perl LWP UserAgent is an amazing tool for automating web queries. Here is the simple text to get HTTP or FTP data using the Perl LWP UserAgent:


use LWP::UserAgent;
use CGI qw(header -no_debug);

# this is an awesome picture, and you wanted to get it via script....
$URL = '';

my $res = LWP::UserAgent->new->request(new HTTP::Request GET => $URL);

# binmode helps when printing data, perl defaults data type as text

# if it's a picture, print the header type and then data.
# if it's not a valid picture, print text/html header type and then the status
# note: this won't work from the command line is meant to be a CGI script!
# words with image/gif only.   Use different headers for different types of image
# or alter code to account for different types.
print $res->is_success ? (header('image/gif'), $res->content)
                        : (header('text/html'), $res->status_line);

Just in case you were curious what it would do on the command line, here is an example:

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: