How to check if a URL is reachable in Perl?
Question
How to check if a URL is reachable in Perl?
Checking if a URL is reachable in Perl generally means verifying whether you can successfully connect to the web server and receive a valid HTTP response. Unlike simple ICMP ping, which sends network packets, URL reachability in the HTTP context usually means making an HTTP request (commonly a GET or HEAD) and inspecting the response code.
Perl offers multiple ways to achieve this, but the most straightforward and beginner-friendly approach uses LWP::UserAgent, a core module for making HTTP requests. It allows you to send requests and check the HTTP status code easily. Note that LWP::UserAgent is part of the standard Perl distribution since Perl 5.8, so you don't need extra modules beyond core installations.
Key Perl Concepts
use strict;anduse warnings;help catch common errors.LWP::UserAgentcreates an agent object that performs HTTP communication.- HTTP response codes: 200 OK means the URL is reachable; anything in the 400- or 500-range usually means failure.
- Sigils:
$for scalar variables (like the $response object), demonstrating typical Perl syntax.
Simple runnable example
#!/usr/bin/perl
use strict;
use warnings;
use LWP::UserAgent;
# The URL to check
my $url = 'http://www.example.com/';
# Create a user agent object
my $ua = LWP::UserAgent->new(
timeout => 5,
agent => "PerlURLChecker/1.0",
);
# Make a HEAD request to check reachability (faster than GET)
my $response = $ua->head($url);
if ($response->is_success) {
print "URL is reachable: ", $response->status_line, "\n";
} else {
print "URL is NOT reachable: ", $response->status_line, "\n";
}
This script:
- Creates a
LWP::UserAgentobject with a 5-second timeout. - Sends an HTTP
HEADrequest (which asks only for headers, not the full content, so it's faster). - Checks the response status with
is_success(true for HTTP 2xx codes). - Prints a helpful message, including the full status line (e.g., "200 OK" or "404 Not Found").
Additional Tips and Gotchas
- HEAD vs GET: HEAD requests are better for checking reachability as they don't download the entire page. However, some servers may not support
HEADand may respond incorrectly, in which case switching toGETcould help. - Timeouts: Set reasonable timeouts so your script does not hang waiting for a slow or unresponsive site.
- HTTPS support:
LWP::UserAgentsupports HTTPS ifIO::Socket::SSLis installed (usually core or easily installable). Without it, HTTPS requests will fail. - DNS and network errors: Errors like unable to resolve the hostname or no network connectivity also count as unreachable.
- Redirects: By default,
LWP::UserAgentfollows up to 7 redirects, so the script will successfully report reachability if the URL redirects to a working page.
Summary
To check if a URL is reachable in Perl, using LWP::UserAgent to send a HEAD or GET request and inspecting the HTTP response is the standard, effective, and cross-platform way. It abstracts away lower-level details like socket programming and ICMP ping permissions, providing a reliable method to verify HTTP connectivity.
Verified Code
Executed in a sandbox to capture real output. • v5.34.1 • 512ms
URL is reachable: 200 OK
(empty)