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

Posibly a weird question but how can you take a string "search&l=phone&s=$query&r=100&m=&p=$cnt" and make it onto ONE url_param. This string is extracted from another page and I'm dynamically writing it to mine.

Only problem I see is with the use of the &. That would force it into seperate paramaters which wouldn't work. Then a thought flew by that I could write out the & as ASCII but realised 1 second later ascii codes have ampersands in them!

So what can be done to make this string one url_param("j")? Making an ID and storing to DB isn't out of question and I could that but that's definitely not preferred.

Replies are listed 'Best First'.
Re: Using url_param()s with &
by jonadab (Parson) on Apr 14, 2005 at 17:08 UTC

    If you want to include an ampersand in a query string argument, the easiest way is to hex-encode it, the same way spaces are done. Spaces (decimal 32, hex 20) are encoded as %20 in URIs, which I'm sure you've seen at some point (here on Perlmonks if nowhere else). I think ampersand would be %26 if I've done the conversion correctly. Of course, whatever you use to get the query args has to know how to unencode this; if you are using a well-known module, that should not be a problem.

    HTH.HAND

    update: I neglected to mention that the browser should do this conversion automatically, if you put the thing in the value attribute of a hidden field (or, for that matter, any form field at all (update2: though yes, you'd need to encode the entities in that case (presumably with HTML::Entities))). However, if you're putting it in an href, then you have to (hex) encode it first. Some of the CGI-related modules may provide functions for doing this (update3: such as URI::Escape), but it's also pretty trivial to do by hand, along the lines of this (which, however, I have not extensively tested):

    $foo =~ s/(\W)/"%".sprintf "%02x",ord$1/eg;

    "In adjectives, with the addition of inflectional endings, a changeable long vowel (Qamets or Tsere) in an open, propretonic syllable will reduce to Vocal Shewa. This type of change occurs when the open, pretonic syllable of the masculine singular adjective becomes propretonic with the addition of inflectional endings."  — Pratico & Van Pelt, BBHG, p68
      There's no need to reinvent this wheel. URI::Escape is part of the Perl "core"

      perl -MURI::Escape -le 'print uri_escape "search&l=phone&s=$query&r=10 +0&m=&p=\$cnt"' __OUTPUT__ search%26l%3Dphone%26s%3D%26r%3D100%26m%3D%26p%3D%24cnt

      Watch out for that $cnt though. I escaped it with \ to keep it from being interpolated, which may or may not be what the OP needs.

        URI::Escape is not part of the Perl core

      The automatic encoding can actually get you into trouble -- because you need to HTML encode the ampersands as & -- so, either of these two would be valid:

      <input type='hidden' value='search&amp;l=phone&amp;s=$query&amp;r=100& +amp;m=&amp;p=$cnt'> <a href='$url?search%26l%3dphone%26s%3d%24query%26r%3d100%26m%3d%26p%3 +d%24cnt'>

      And once you realize that, it makes perfect sense why there's the push to use ; as its seperator (which you don't need to HTML escape)

Re: Using url_param()s with &
by sulfericacid (Deacon) on Apr 14, 2005 at 17:19 UTC
    Really all you would need to do is change the ampersands to some character or small series of characters you know will never appear in the string (say: XAS;) and just do a little s/// when you need to extract the param to return it to its original form.


    "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

    sulfericacid