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

I know this subject has come up recently in other contexts, and Merlyn was very detailed on the subject, but can I ask it quickly and get a quick answer, just to get going on my project?

I have search engine I've written.

I have a "10 results per page" format.

When the user searches for "what's up doc?" and gets more than ten results, I want them to be able to go to the next page (results 11 to 20) and also to populate the search field on that page with their query. So I need the string to be encoded.

What's the correct module and command to use to transform "what's up doc?" with its spaces and punctuation, into something that's legal for a query string, that is, not:

myscript.cgi?query=what's up doc?&page=2
TIA,
--
“Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
M-J D

Replies are listed 'Best First'.
Re: URL/URI encoding strings to go into query strings
by dakkar (Hermit) on Feb 09, 2003 at 13:10 UTC

    Instead of URI::Escape, you can use URI::URL:

    use URI::URL; my $u=URI::URL->new('/myscript.cgi'); $u->query_form(query=>q(what's up doc?), page => 2); print $u->as_string,"\n";

    prints:

    /myscript.cgi?query=what's+up+doc%3F&page=2
    -- 
            dakkar - Mobilis in mobile
    
Re: URL/URI encoding strings to go into query strings
by steves (Curate) on Feb 09, 2003 at 06:11 UTC
    URI::Escape can be used to escape and unescape "special" characters in URI's.

      URI::Escape can be used to escape and unescape "special" characters in URI's.

      URI::Escape should be used to escape and unescape "special" characters in URI's query strings. :)

      Just me being quite picky (hey, I just got home from work at 3:30AM. I'm allowed to be picky {grin}). All I mean here is that URI::Escape should always be used if you are planning on passing anything more trivial than "a=bc" as a query string. Also note that URI::Escape should only be used on the query string, not the entire URI. So you'd do this:

      print 'http://www.perlmonks.org/index.pl?node=', uri_escape('Am I #1? +Yeah!');

      rather than this:

      print uri_escape('http://www.perlmonks.org/index.pl?node=Am I #1? Yeah +!');

      Why? Because the latter will also escape the colon, slashes, the question marks, and even that '=' sign (I hate that last one). The annoying thing I find about URI::Escape is the fact that you have to be careful what part of the query string you pass it, as it will sometimes give undesired results. I already know that you knew that steves, just me being picky and trying to clarify something that might already be clear to readers :). ++ to you for recommending the right module for the job :)


      If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, reply to this node or /msg me to tell me what is wrong with the post, so that I may update the node to the best of my ability. If you do not inform me as to why the post deserved a downvote, your vote does not have any significance and will be disregarded.

Re: URL/URI encoding strings to go into query strings
by Jenda (Abbot) on Feb 09, 2003 at 21:27 UTC
    use CGI::Enurl; $url = 'myscript.cgi?' . enurl( {query => "what's up doc?", page => 2, });
    See CPAN

    Jenda

Re: URL/URI encoding strings to go into query strings
by Cody Pendant (Prior) on Feb 09, 2003 at 20:36 UTC
    Thank you both for your help.
    --
    “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
    M-J D
Re: URL/URI encoding strings to go into query strings
by tomhukins (Curate) on Feb 10, 2003 at 13:09 UTC
    I realise this does not answer your question, but do you know about Data::Page and Data::Pageset? You might find one of them useful in this situation.