Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Trying to parse a URL string

11 views
Skip to first unread message

Angela Barone

unread,
Mar 24, 2013, 2:48:28 PM3/24/13
to Beginner Perl
Hello,

I'm trying to do the following, but I'm not having much luck.

------------
use strict;
use warnings;
use 5.010;
use CGI qw(param);

my %q;
my $referrer = 'http://www.bing.com/search?q=stainless+wire+rope&t=hhhh';

my $q = CGI->new($referrer);
say '$q = '. $q->param('q'); # 'q' is empty
------------

How can I get the 'q' parameter from $referrer? In case it matters, this variable is coming from a db.

Thank you,
Angela

A2 Hosting now has Perl 5.10.1
http://www.a2hosting.com/1250.html




David Precious

unread,
Mar 24, 2013, 2:54:45 PM3/24/13
to Beginner Perl
On Sun, 24 Mar 2013 11:48:28 -0700
Angela Barone <ang...@italian-getaways.com> wrote:

> Hello,
>
> I'm trying to do the following, but I'm not having much luck.
>
> ------------
> use strict;
> use warnings;
> use 5.010;
> use CGI qw(param);
>
> my %q;
> my $referrer =
> 'http://www.bing.com/search?q=stainless+wire+rope&t=hhhh';
>
> my $q = CGI->new($referrer);
> say '$q = '. $q->param('q'); # 'q' is empty
> ------------
>
> How can I get the 'q' parameter from $referrer? In case it
> matters, this variable is coming from a db.

URI and URI::QueryParam will do exactly what you need:

use URI;
use URI::QueryParam;

my $uri = URI->new($referrer);
my $q = $uri->query_param('q');




--
David Precious ("bigpresh") <dav...@preshweb.co.uk>
http://www.preshweb.co.uk/ www.preshweb.co.uk/twitter
www.preshweb.co.uk/linkedin www.preshweb.co.uk/facebook
www.preshweb.co.uk/cpan www.preshweb.co.uk/github


Angela Barone

unread,
Mar 24, 2013, 9:53:06 PM3/24/13
to David Precious, Beginner Perl
On Mar 24, 2013, at 11:54 AM, David Precious wrote:
> URI and URI::QueryParam will do exactly what you need:
>
> use URI;
> use URI::QueryParam;
>
> my $uri = URI->new($referrer);
> my $q = $uri->query_param('q');

Hi David,

Thank you so much for this. It works perfectly. I had seen something on URI before but it looked a lot more complex that what it turned out to be.
0 new messages