Yes, you can create an HTTP client in Perl using the LWP (Libraries for Perl Web) module. Here's a simple example of how to use it to send an HTTP request and retrieve the response:
use LWP::UserAgent;
my $ua = LWP::UserAgent->new();
my $req = HTTP::Request->new(GET => 'http://www.example.com');
my $res = $ua->request($req);
print $res->status_line, "\n";
print $res->content, "\n";
This code creates a new LWP::UserAgent object, which is used to send the HTTP request. The HTTP::Request object is used to define the request, specifying the HTTP method (in this case, GET), the URL, and any other headers or parameters that need to be sent with the request.
The request method of the LWP::UserAgent object is then called, passing in the HTTP::Request object. This sends the request to the server and returns a response object, which is stored in the $res variable.
The status_line method of the response object is used to retrieve the HTTP status line (e.g. "200 OK"), and the content method is used to retrieve the response body.
You can also add headers to the request using the header method of the HTTP::Request object, like this:
my $req = HTTP::Request->new(GET => 'http://www.example.com');
$req->header( 'User-Agent', 'My Perl Script' );
This sets the User-Agent header to "My Perl Script".
You can also use the post method to send a POST request, and the put method to send a PUT request.
my $req = HTTP::Request->new(POST => 'http://www.example.com',
Content => [
'key' => 'value',
'key2' => 'value2',
],
);
This sends a POST request to http://www.example.com with two parameters key and key2 with values value and value2 respectively.
You can also use the LWP::UserAgent object to send HTTPS requests by specifying the URL as https://www.example.com.
my $ua = LWP::UserAgent->new();
my $req = HTTP::Request->new(GET => 'https://www.example.com');
my $res = $ua->request($req);
This sends a GET request to https://www.example.com using the LWP::UserAgent object.
There are many other options and methods available in the LWP module that you can use to customize your HTTP requests and handle the responses. You can read more about it in the LWP documentation.