Perl parse url to get value using

How to get the value of the 'code' parameter using the URI :: URL perl module from this link: http://www.someaddress.com/index.html?test=value&code=INT_12345 This can be done using the URI :: URL or URI ( I know that the first one is outdated). Thanks in advance.

+4
source share
2 answers

Create a URI and use the query_form method to get the key / value pairs for the query. If you know that the code parameter is specified only once, you can do it as follows:

 my $uri = URI->new("http://www.someaddress.com/index.html?test=value&code=INT_12345"); my %query = $uri->query_form; print $query{code}; 

Alternatively, you can use the URI :: QueryParam, which adds additional methods to the URI object:

 my $uri = URI->new("http://www.someaddress.com/index.html?test=value&code=INT_12345"); print $uri->query_param("code"); 
+8
source
 use URI; my $uri = URI->new("http://someaddr.com/index.html?test=FIRST&test=SECOND&code=INT_12345"); my %query = $uri->query_form; use Data::Dumper; print Dumper \%query; 

We can see:

  $VAR1 = { 'test' => 'SECOND', 'code' => 'INT_12345' }; 

Unfortunately, this result is incorrect.

Possible Solution:

 use URI::Escape; sub parse_query { my ( $query, $params ) = @_; $params ||= {}; foreach $var ( split( /&/, $query ) ){ my ( $k, $v ) = split( /=/, $var ); $k = uri_unescape $k; $v = uri_unescape $v; if( exists $params->{$k} ) { if( 'ARRAY' eq ref $params->{$k} ) { push @{ $params->{$k} }, $v; } else { $params->{$k} = [ $params->{$k}, $v ]; } } else { $params->{$k} = $v; } } return $params; } 
+4
source

Source: https://habr.com/ru/post/1434197/


All Articles