Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

encoding URL ampersands?

by Anonymous Monk
on Jul 29, 2010 at 19:36 UTC ( [id://851995]=perlquestion: print w/replies, xml ) Need Help??

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

This is my first project in Perl, so be kind.

I have constructed a URL which contains a URL as the value to a query parameter. So, some ampersands need to be treated as ampersands separating query parameters, but others need to be changed so they aren't interpreted as separating query parameters.

Here's an example:

my $url_parm = 'http://www.whatzit.com?a=1&b=2'; $url =~ s/&/&/sg; my $url = "http:/www.xyzzy.com?url=$url";

Can someone point out the error of my ways?

Thanks.

Replies are listed 'Best First'.
Re: encoding URL ampersands?
by kennethk (Abbot) on Jul 29, 2010 at 19:49 UTC
    If I am doing any sort of encode/decode operation, I try and use modules rather than rolling my own. In this case, I would use URI::Escape to handle it - chances are a well-used module is going to handle corner cases I would have missed.

    #!/usr/bin/perl use strict; use warnings; use URI::Escape; my $url_parm = uri_escape 'http://www.whatzit.com?a=1&b=2'; my $url = "http:/www.xyzzy.com?url=$url_parm"; print $url, "\n";

    To comment on the posted code, I assume you accidentally used $url where you meant to use $url_param, a la:

    my $url_parm = 'http://www.whatzit.com?a=1&b=2'; $url_parm =~ s/&/&/sg; my $url = "http:/www.xyzzy.com?url=$url_parm";

    This type of error will be caught by the strict pragma. It would likely be worth your while to read Use strict warnings and diagnostics or die and Basic debugging checklist.

    Note that you have used the wrong encoding for the task you describe - you changed to HTML_entities instead of Percent_encoding. You also missed several characters that are not legal in URIs. Exactly the reason for using existing, well tested modules.

Re: encoding URL ampersands?
by eff_i_g (Curate) on Jul 29, 2010 at 19:39 UTC
Re: encoding URL ampersands?
by ikegami (Patriarch) on Jul 29, 2010 at 20:16 UTC

    & => & is part of HTML and XML's escaping scheme. URIs use a different escape mechanism. URI::Escape provides it.

    my $url_param = 'http://www.whatzit.com?a=1&b=2'; my $url = "http:/www.xyzzy.com?url=" . uri_escape($url_param);

    If you are placing the constructed URL in HTML or XML, you will also need to HTML-escape the constructed URL as a whole.

    [ Wrote this much earlier, but forgot to actually post it. Sorry if it's redundant ]

Re: encoding URL ampersands?
by almut (Canon) on Jul 29, 2010 at 19:49 UTC

    You probably meant

    my $url_parm = 'http://www.whatzit.com?a=1&b=2'; $url_parm =~ s/&/&/sg; my $url = "http:/www.xyzzy.com?url=$url_parm";
Re: encoding URL ampersands?
by aquarium (Curate) on Jul 30, 2010 at 02:05 UTC
    most of the perl cgi modules (including the perl module named CGI) provide url parameter getter and setter functions that take care of encoding etc, so you don't need to re-invent the wheel--unless you really do need to. example
    use CGI qw(:standard); if(param()) { if($a=param('a')) { /* code for url parameter a */ } } else { /* generate html form when not called with parameters */ /* the form submit url points to itself (the .pl) */ }
    The first if checks that there are parameters present, which is very handy as you can attach code to the else for this if, to generate a form. In other words, the same perl script processes form/url parameters and data submitted OR generates a html form to fill in.
    the hardest line to type correctly is: stty erase ^H
Re: encoding URL ampersands?
by sundialsvc4 (Abbot) on Jul 30, 2010 at 00:08 UTC

    P.S.   You can always count on receiving “kindness” here, so when you need help, don’t be afraid to ask.   Perlmonks is a remarkably professional community.   (I think it has something to do with the programming language that we love to use ...)   :-)

    Incidentally, you probably want to ask frequently, because another principle of Perl is:   TMTOWTDI = There's More Than One Way To Do It.   Most of the problems that you are likely to need to address using the Perl language have already been solved, and solved very well, in several different ways.   Therefore, you very often find that you spend more time searching for a suitable solution that already exists, and comparatively much less time “cooking something up.”

    This happens so frequently, in fact, that if you do find yourself reaching for those ol’ cooking utensils, your first instinct should be to step back and do some surfing ... to this site, and to CPAN.   And as I said, always feel free to ask the community for their professional opinion.   (Think it through well-enough to pose an answerable question, maybe with some alternatives you’ve been considering, and fire away.)

    Actume Ne Agas:   Do Not Do A Thing Already Done.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://851995]
Approved by kennethk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-19 07:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found