Yadheendran Yadheendran - 4 months ago 28
Perl Question

Pass perl file arguments to LWP HTTP request

Here is my issue with handling argument Perl. I need to pass Perl argument argument to a http request (Webservice) whatever Argument given to the perl file.

perl wsgrep.pl -name=john -weight -employeeid -cardtype=physical

In the wsgrep.pl file, i need to pass the above arguments to http post params.

like below,


I am using LWP Package for this url to get response.

Is there any good approach to do this?

Inside the wsgrep.pl

my ( %args, %config );

my $ws_url =

my $browser = LWP::UserAgent->new;
# Currently i have hard-coded the post param arguments. But it should dynamic based on the file arguments.
my $response = $browser->post(
'name' => 'john',
'cardtype' => 'physical'

if ( $response->is_success ) {
print $response->content;
else {
print "Failed to query webservice";
return 0;

I need to construct post parameter part from the given arguments.

'name' => 'john',
'cardtype' => 'physical'


Normally, to url-encode params, I'd use the following:

use URI;

my $url = URI->new('http://example.com/query');

say $url;

Your needs are more elaborate.

use URI         qw( );
use URI::Escape qw( uri_escape );

my $url = URI->new('http://example.com/query');

my @escaped_args;
for (@ARGV) {
   my ($arg) = /^-(.*)/s
      or die("usage");

   push @escaped_args,
      join '=',
         map uri_escape($_),
            split /=/, $arg, 2;

$url->query(@escaped_args ? join('&', @escaped_args) : undef);

say $url;