Call: (480) 382-8464

Perl LWP UserAgent Example: Get Picture Using LWP

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:

#!/usr/bin/perl

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

# this is an awesome picture, and you wanted to get it via script....
$URL = 'http://digitalcrunch.com/wp-content/uploads/jamesfraze1.jpg';

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

# binmode helps when printing data, perl defaults data type as text
binmode(STDOUT);

# 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:

Tags: ,

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Main Offers/Services

Hosting and website related services offered by Digital Crunch:



Managed VPS Hosting

Server Updates, Module/Software Installs, Firewall, Security, Hosting Management, Wordpress - let us manage it for you

Hosted Email

Setup and Maintain Business Class, Secure Email Hosting including spam protection and 25GB of storage space

Linux Consulting

Server Updates, Module/Software Installs, Firewall, Security, Hosting, Wordpress and Apps, anything related to Linux

Email Marketing Services

We broadcast your content to your list, capture leads for you, follow up on schedules, make more sales for you

Website Maintenance

Content updates, menu changes, graphics changes, adding content, getting links, building traffic, building sales funnels

Hosting Tutorials

Tutorials we've written for other customers that like to get their hands dirty and learn about hosting