URL Parsing

  • Thread starter Francis Sylvester
  • Start date
F

Francis Sylvester

Hi,

I'm trying to parse a URL string with the help of the CGI module. I'm sure
this is really simple but I just can't seem to retrieve the first variable
after the question mark in the URL - my code returns nothing for this
variable - variables after this are printed as expected. I've tried looking
at the perldoc but I still can't figure out why this isn't working... Any
help appreciated!

Thanks,
Francis.

# Example Code

use warnings;
use strict;
use CGI;

my $URL =
"http://.someurl.com/somepath?test1=testresult1&test2=testresult2&test3=testresult3";

my $test = new CGI($URL);
print $test -> param('test1');
print $test -> param('test2');
print $test -> param('test3');
 
U

usenet

Francis said:
use CGI;

my $URL =
"http://.someurl.com/somepath?test1=testresult1&test2=testresult2&test3=testresult3";

my $test = new CGI($URL);
print $test -> param('test1'); [#empty!]

That's not how the "new" method works. It doesn't really make sense to
create a CGI object with an URL. Try:

print $test -> param('http://.someurl.com/somepath.cgi?test1');

and you'll see what's happening.

You could also do this:

print $test -> Dump;

which would show you the structure of your params.
 
T

Todd W

Francis Sylvester said:
Hi,

I'm trying to parse a URL string with the help of the CGI module. I'm sure
this is really simple but I just can't seem to retrieve the first variable
after the question mark in the URL - my code returns nothing for this
variable - variables after this are printed as expected. I've tried looking
at the perldoc but I still can't figure out why this isn't working... Any
help appreciated!

Thanks,
Francis.

# Example Code

use warnings;
use strict;
use CGI;

my $URL =
"http://.someurl.com/somepath?test1=testresult1&test2=testresult2&test3=test
result3";

my $test = new CGI($URL);
print $test -> param('test1');
print $test -> param('test2');
print $test -> param('test3');

None of the constructors documented at
http://search.cpan.org/~lds/CGI.pm-3.11/CGI.pm support accepting a full URL
as an argument. What would make you think what you are trying to do is
possible?

CGI.pm does provide a constructor that accepts a query string as an
argument, so you could pass the part after the "?" to ->new and your code
would work as desired.

Todd W.
 
A

attn.steven.kuo

Francis said:
Hi,

I'm trying to parse a URL string with the help of the CGI module.
(snipped)


use CGI;

my $URL =
"http://.someurl.com/somepath?test1=testresult1&test2=testresult2&test3=testresult3";

my $test = new CGI($URL);
print $test -> param('test1');
print $test -> param('test2');
print $test -> param('test3');


Try the URI and URI::QueryParam modules from CPAN:

use URI;
use URI::QueryParam;

my $u = URI->new(
"http://.someurl.com/somepath?test1=testresult1&test2=testresult2"
);

for my $key ($u->query_param)
{
print "$key: ", join(", ", $u->query_param($key)), "\n";
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,501
Latest member
Ledmyplace

Latest Threads

Top