Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

using hash to lookup value

by grashoper (Monk)
on Oct 16, 2007 at 02:26 UTC ( [id://645097]=perlquestion: print w/replies, xml ) Need Help??

grashoper has asked for the wisdom of the Perl Monks concerning the following question:

I need to create a subroutine that will readin the value of the querystring parse the query string and redirect the user to a helptopic, I am not sure how to accomplish theparsing and redirection, this is for a large pre-existing website,which I am now in charge of maintaining so far I have this..
Process form variables for both POST and GET methods. # Below Will process all query string variables. if ($ENV{'REQUEST_METHOD'} eq "GET" || $ENV{'QUERY_STRING'} ne "") { @pairs = split(/\&/, $ENV{'QUERY_STRING'}); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); # Split into name and value. $$name = $value; # Assign value to scalar matching name. $$name =~ s/%(..)/chr(hex($1))/ge; # Decode encoded stuff. $$name =~ s/\+/ /g; # substitute +'s for spaces. } } #Home page (and default opening topic): TEMPO 5 Help #Topics-01-1.html #Home would be returned in the querystring as value for topic..
I am sure this is a pretty easy question but I am really new and don't totally understand how to access hash elements to get what I need.

Replies are listed 'Best First'.
Re: using hash to lookup value
by naikonta (Curate) on Oct 16, 2007 at 02:54 UTC
    Rolling your own CGI parameter parsing is not recommended but nothing stops from you doing that. Well, if you insist....
    my %params; # declare a hash to hold the params later if ($ENV{REQUEST_METHOD} eq 'GET' && $ENV{QUERY_STRING} ne '') { .... foreach .... { my($name, $value) = split .... # get the name and value ..... # cleaning name and value $params{$name} = $value; # hash assignment } }
    But, if you don't mind to use a CPAN module (I do recommend it), you can use CGI.pm or CGI::Simple. I assumed that the topic is requested with topic parameter (such as http://www.example.com/cgi-bin/help.cgi?topic=user) and that each topic has a corresponding html file.
    #!/usr/bin/perl use strict; use warnings; use CGI::Simple; my $baseurl = 'http://www.example.com/helps'; my %available_topics = ( home => 'home.html', user => 'user.html', add => 'add.html', update => 'update.html', remove => 'remove.html', #... other help topics ); my $default_topic = 'home'; my $cgi = CGI::Simple->new; # let CGI::Simple does the parsing my $topic = $cgi->param('topic') || ''; # get the wanted topic # get corresponding html file my $help_file = $available_topcs{$topic} || $available_topics{$default +_topic}; # send redirection print $cgi->redirect("$baseurl/$help_file");

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

      Thanks for the quick response, we are using activestate perl and pages are being output via xml in different locations in this webapp that is the site..xmlwriter outputs some of it, it uses xslt transforms but I am not well versed in that either so not sure how to pass a query string in one of those (I know its supposed to be possible but I don't understand it well enough to want to attempt it) data stored in sql for each site etc, the helpfiles themselves are populated into an iframe they are just html, could this be done without using cgi? This section I am working on goes from our webapplication to our help website its passing authentication from the webapp as well as where in our application they are,my goal is to make the help "context" sensitive based on what is returned in that string and make it appear on the page. your assumptions are correct in regards to the topic. time is an issue here as well, they want this working right away, which of course doesn't help my learning curve much .. many thanks for the sample, I will work on today and write back if I am still stupid tomorrow..I probly will be..lol..

        Or if you don't like CGI.pm (why?):

        print "Location: "$baseurl/$help_file\n\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://645097]
Approved by naikonta
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-19 00:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found