Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Best way to generate URLs in CGI script?

by bikeNomad (Priest)
on Jul 27, 2001 at 05:46 UTC ( #100201=perlquestion: print w/replies, xml ) Need Help??

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

I just started my first CGI script. Everything was working OK until I came to the part where I had to generate a reference to different CGI (in this case, graph?channel=1&xsize=400&ysize=300

Well, I said, there's this huge (6500 line+); it must be good for something. So I tried constructing a new CGI object to generate the escaped URL and couldn't find any way to express a different URL (I could change its parameters, but not its address). Hmm... I found the URI module, and it looks like it'll do the job.

But isn't there a clever way to take a base address and a bunch of name=value parameters and come up with a URL within the massive module? I realize I could roll my own with url_escape etc, but this has got to be something that's pretty common. What do you do?

Replies are listed 'Best First'.
Re: Best way to generate URLs in CGI script?
by Kanji (Parson) on Jul 27, 2001 at 06:36 UTC

    The trick -- if you really want to use to do this -- is to override what believes the script name is, which can be done temporarily with local...

    use CGI '-oldstyle_urls'; my $cgi1 = CGI->new; { local $ENV{'SCRIPT_NAME'} = '/graph'; my $cgi2 = CGI->new({ 'channel' => 1, 'size' => 400, 'ysize' => 300, }); print $cgi2->url( -query_string => 1 ), "\n"; } print $cgi1->url( -query_string => 1 ), "\n";

    The only caveat is to be really careful with your scoping: put the wrong thing in the wrong place, and it prolly won't work the way you expect.



      I wonder about the design of; what good is having the object-oriented interface if it's dealing mostly with global variables anyway? Sure, it doesn't have to export names into main::, but what's the point? The HTML generation stuff doesn't access any object state, so it's pointless as instance methods. The url escaping stuff, likewise. So what really needs to be instance methods? Just things dealing with the current (global) query. So why is the whole mess in a single class?

        The HTML stuff using object state is very helpful if you want to subclass it.

        Contrived example, but for instance, if you want to keep track of the number of cells in the previous row so you can colspan the current row without hard coding it. (For tables that can change dynamically, you can provide your own Tr Td methods to track this.


        "To be civilized is to deny one's nature."
Re: Best way to generate URLs in CGI script?
by synapse0 (Pilgrim) on Jul 27, 2001 at 05:59 UTC
    basically, the creating a new object should have worked..
    $cgi = new CGI; #your basic info from QUERY_STRING or STDIN $other_cgi = new CGI($query_string); # $query_string is the url info t +o escape
    then use $other_cgi->param($param) to obtain the params...
    $other_cgi is a different object than $cgi at this point..
    Hope that helps..
      I tried this first, but what happened is that the query string got interpreted as a keyword. Let's say that I wanted the (relative) URL to be graph?a=b&c=d :

      my $target = CGI->new( 'graph' ); $target->param( -a => 'b', -c => 'd' ); $target->url; # 'http://localhost/cgi/originalPath' $target->query_string; # 'keywords=graph' $target->param; # 'keywords'

      So this didn't work...

        Ok.. well I can honestly say I'm not sure what you're trying to do then.. are you trying to call an external program and give it the query string?? if so, you can just set $ENV{QUERY_STRING} to your string, and exec the other cgi.. Otherwise, I need a little clarification..
        (i'm at work right now and only half paying attention, that may have something to do with it...)
Re: Best way to generate URLs in CGI script?
by rrwo (Friar) on Jul 27, 2001 at 05:53 UTC

    Try the URI::Escape module.

Re: Best way to generate URLs in CGI script?
by shotgunefx (Parson) on Jul 27, 2001 at 23:45 UTC
    In the past, for calling another script, I generate the cgi object with the params I need and call self_url, then I substitute the new script url for the current one.
    my $newscript = ""; my $url = $q->self_url; $url =~s/^.*?\?//; # Remove url portion $newscript.=$url;


    "To be civilized is to deny one's nature."

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://100201]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2021-05-14 22:31 GMT
Find Nodes?
    Voting Booth?
    Perl 7 will be out ...

    Results (150 votes). Check out past polls.