Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

query string

Status
Not open for further replies.

motte

Programmer
Joined
May 31, 2001
Messages
155
Location
US
How do I pull a value out of a query string?
Suppose I have at the end of a url:
foo.cgi?name=Mike

How do I get Mike into a variable? I've done it with asp but I am not sure how to do it with perl. I haven't been successful looking for documentation on it.

Mike
 
Mike,

The easiest way to do this is by using the CGI module. I am not going to get into the specifics of how it works but you can do the following.

use CGI;

my $form = new CGI;

my $name = $form->param('name');

This will assign name to Mike. You can do this for any of the variables sent. If you have multiple varibles sent you would use an array.

my @names = $form->param('name');

There is plenty of documentation on the CGI module. This should get you started.

Brandon
 
Assuming the URL is stored in a variable $URL, the following should work:

($Var) = $Url =~ m/name=(\w+)/;
 
Brandon,

Will this work for something sent via the query string? I am using the CGI module right now, but I want to pass one thing in the url. The second page will grab this bit of info from the url. Would I need to create a new CGI object on this page to get the data from the query string?

Mike
 
Raider,

How does the url get stored in a variable? I don't understand your thread.

Mike
 
Mike,

I am a little unsure what you are asking as well. Lets assume you have the following query string

?name=mike&color=blue&language=perl

You can use the following for each variable.

$name = $form->param('name');
$color = $form->param('color');
$language = $form->param('language');

This will assign the proper data to each variable. Is this what you are looking for?


Brandon
 
Brandon,

I figured out what I was looking for. But I will explain what I did anyways.

I had a form posting to the same page, different subs, based on a hidden value. It creates a text file and shows it in the browser. Since the text file has html tags in it, it displays that text file as html. However, it was adding the html, head, title, body, and closing of each around the text file that already has those tags. That page, when the source is viewed, shows the duplicate tags. So I've set up a link with ?pagename=foo to a new cgi page, passing along the name of the text file I want to view. That page gets foo and will open the text file, displaying it to the browser. The key for me was getting it over to that page, which I just did, and making sure the content type was text/plain. I hope that lets you know what I was trying to do. Thanks for your help.

Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top