How to use LWP::Simple for quick GET requests in Perl?
Question
How to use LWP::Simple for quick GET requests in Perl?
LWP::Simple is a lightweight Perl module designed for making quick HTTP GET requests without the overhead of the full LWP::UserAgent. It provides simple functions like get, head, and getprint to fetch web content easily. This makes it ideal for beginners or scripts that just need to retrieve a URL's content rapidly.
Using LWP::Simple for Quick GET Requests
The most common function you'll use from LWP::Simple is get. It takes a URL and returns the content of that URL as a string in scalar context. If the request fails, it returns undef. This simplicity means you can fetch a web page with just a couple of lines.
Here’s a quick rundown of useful LWP::Simple functions:
get($url): Fetches the URL content as a string.getprint($url): Fetches and immediately prints the content.head($url): Fetches HTTP headers from the URL.getstore($url, $file): Saves the content to a file.
LWP::Simple uses simple scalar context to return content, so understanding Perl's context is useful here, as the return varies by context.
Basic Example: Fetch and Print Web Page Content
use strict;
use warnings;
use LWP::Simple;
my $url = 'http://www.example.com';
# Fetch the content of the URL
my $content = get($url);
if (defined $content) {
print "Successfully fetched content from $url\n";
print "---- Start of content ----\n";
print $content;
print "\n---- End of content ----\n";
} else {
warn "Failed to fetch content from $url\n";
}
Save this as a Perl script and run it. You’ll see the HTML content of http://www.example.com printed to STDOUT.
Important Perl Concepts Highlighted
- Sigils:
$is used becausegetreturns a scalar string. - Context:
getbehaves differently in scalar vs list context; it returns the entire content as a scalar here. - TMTOWTDI (“There’s More Than One Way To Do It”): Perl’s philosophy shines with many ways to fetch content;
LWP::Simpleprovides the simplest.
Common Pitfalls and Notes
LWP::Simplesilently returnsundefon failure; always check for it before using the content.- It does not provide advanced HTTP features like headers control or HTTPS certificate options; for more control, use
LWP::UserAgent. - Modules such as
HTTP::Tiny(core since 5.14) can also be used for simple HTTP requests in modern Perl. - Be aware of the URL scheme:
LWP::Simplesupports HTTP and HTTPS URLs.
Summary
For quick and easy HTTP GET requests in Perl, LWP::Simple is a great first choice. Its get function returns the web page content in only a few lines of code, with minimal setup. Just remember to check for failure with defined, and switch to LWP::UserAgent if you need more control.
Verified Code
Executed in a sandbox to capture real output. • v5.34.1 • 1654ms
Successfully fetched content from http://www.example.com
---- Start of content ----
<!doctype html><html lang="en"><head><title>Example Domain</title><meta name="viewport" content="width=device-width, initial-scale=1"><style>body{background:#eee;width:60vw;margin:15vh auto;font-family:system-ui,sans-serif}h1{font-size:1.5em}div{opacity:0.8}a:link,a:visited{color:#348}</style><body><div><h1>Example Domain</h1><p>This domain is for use in documentation examples without needing permission. Avoid use in operations.<p><a href="https://iana.org/domains/example">Learn more</a></div></body></html>
---- End of content ----
(empty)Was this helpful?
Related Questions
- How to handle gzip compressed responses with LWP in Perl?
- How to parse HTML response with HTML::TreeBuilder in Perl?
- How to use Mojo::UserAgent for HTTP requests in Perl?
- How to set connection keep-alive with LWP::UserAgent in Perl?
- How to use LWP::Protocol::https for HTTPS requests in Perl?
- How to handle HTTP errors and exceptions with LWP in Perl?