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

I'm trying to find some existing way to simply add a parameter (name=value) pair to a user-defined URL. I have a function that accepts a URL, a name, and a value, and I want to append the name=value pair to that URL (not the url returned by CGI::self_url) and return the new URL to the user. I haven't seen any existing way to do this - does anyone know of a way to easily do this, or do I have to write up something new?
I'm not sure if its sufficient to simply append the string '&name=value' to a URL containing a '?', or attach '?name=value' to a URL without a '?' - if it is I could obviously write my own subroutine faster than I've written this message. The subroutine needs to work for any valid URL.
Thanks for any help in advance.
Leigh

Replies are listed 'Best First'.
•Re: Adding param to a specified URL
by merlyn (Sage) on Mar 28, 2002 at 14:36 UTC
    use URI; my $user_url = "http://www.stonehenge.com/cgi/dilbert"; my $params = [ max => 5, scary => "%&foo;%" ]; # ... my $uri = URI->new($user_url); $uri->query_form(@$params); print $uri->as_string;
    prints...
    http://www.stonehenge.com/cgi/dilbert?max=5&scary=%25%26foo%3B%25
    I presume that's what you're looking for. If not, ask again. URI is part of LWP, which you should have installed anyway. {grin}

    -- Randal L. Schwartz, Perl hacker

      I failed to mention that I cannot replace existing parameters with the new one I am appending. The above code does this. So I have written the below which appears to work:
      use strict;
      use URI::URL;
      
      sub addEnvVar ($$$$)
      {
          my $URL = shift;
          my $name = shift;
          my $value = shift;
          my $returnRef = shift;
      
          # Create the URL as an object and extract already existing parameters.
      
          my $url = new URI::URL ($URL);
          my $qstring = $url->equery ();
      
          # Split the parameters up.
      
          my @params = split (/\&/, $qstring);
      
          # Split the name=value pairs up and place them in a parameter list.
      
          my @paramList;
          foreach my $param (@params)
          {
      	my ($pname, $pvalue) = split (/\=/, $param);
      	push (@paramList, $pname);
      	push (@paramList, $pvalue);
          }
      
          # Add the new parameters to the end of the list.
      
          push (@paramList, $name);
          push (@paramList, $value);
      
          # Place the parameter list in the URL.
      
          $url->query_form (@paramList);
      
          # Get the resulting URL after appending the new variable.
      
          my $newURL = $url->as_string ();
          $$returnRef = $newURL;
      
          return 1;
      }
      
      The only minor problem I had testing this occurred when I tried to have parameters such as:
         carr%20ots=cooked
      These would appear in the resulting URL as:
         carr%2520ots=cooked
      But I'm not sure if its even legal to have such things in
      parameters.  I think I'll stick with this for now.
      Thanks a lot for your help,
      
      Leigh
      
        No no no. Far too much work.
        use URI; sub add_param { my $url = shift; # string my $name = shift; # string my $value = shift; #string $url = URI->new($url); $url->query_form($url->query_form, $name, $value); return $url->as_string; }
        But the problem is if you're going to call this more than once, you really ought to hoist it up to the outer level, or give it more than one pair at a time, or it will be doggy slow. </code>

        -- Randal L. Schwartz, Perl hacker

      Aw, darn, adding scary to the url doesn't do anything interesting :) (I was hoping for some odd easter egg, I guess. It's that season)

      BTW, in case no one's mentioned this to you yet today, that dilbert page ROCKS. Thanks!
      --
      Mike

Re: Adding param to a specified URL
by andye (Curate) on Mar 28, 2002 at 14:36 UTC
    I'm not sure if its sufficient to simply append the string '&name=value' to a URL containing a '?', or attach '?name=value' to a URL without a '?'

    Yup. :)

    (If you're asking what I think you're asking, that is). Name/value pairs can be added to a GET request in just the way you suggest.

    hth,
    andy.

    Update: Of course, this won't work if the script behind the URL is expecting to receive parameters by POST rather than GET.

Re: Adding param to a specified URL
by projekt21 (Friar) on Mar 28, 2002 at 14:40 UTC
    I'm not sure if its sufficient to simply append the string '&name=value' to a URL containing a '?', or attach '?name=value' to a URL without a '?'
    This looks good so far, but according to rfc2396 there might be a problem with fragments (#) like in: http://www.foo.org/cgi-bin/bar.pl?key=value#top

    alex pleiner <alex@zeitform.de>
    zeitform Internet Dienste