g-monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
I was hoping someone could pass on the wisdom of how to handle a url where one of the param values contains an ampersand.
e.g. &genre=Journal&title=Molecular & cellular biomechanics&atitle=....

My program currently would handle the url like so;

foreach my $key (@keys) { print "$key - " . param($key) ." <br>"; }

output...
genre - Journal
title - Molecular
cellular biomechanics -

Thanks for your help.
Gary

Replies are listed 'Best First'.
Re: param() and param value that contains &
by CountZero (Bishop) on Nov 10, 2010 at 06:55 UTC
    The ampersand must be "escaped" or "encoded": see URL with query string.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: param() and param value that contains &
by ikegami (Patriarch) on Nov 10, 2010 at 06:56 UTC
    Your URL is broken. It should be
    ...&title=Molecular%20%26%20cellular%20biomechanics&atitle=...

    Your HTML is broken too. You don't convert the text into something HTML will consider text.

      I cannot control the formatting of the URL as it is generated by another system.

      I think I will have to use $ENV{'QUERY_STRING'} and try retrieving the params and values that way.

      Thanks for your input,
      Gary

        Indeed. CGI parses the URL queries as it they were x-www-form-urlencoded format(*). Since your URL queries is in another format, you'll have to find yourself an appropriate parser for that format.

        * — With some useful deviations that aren't relevant here.