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

i tried url escape bt failed to work for me.

#!/usr/bin/perl use strict; use URI::Escape; print uri_escape( (name=>"josh",age=>26)); #output: name=josh&age=26

Replies are listed 'Best First'.
Re: url escape
by davido (Cardinal) on Apr 22, 2015 at 15:07 UTC

    I think you are just trying to construct the query segment of a URI, probably for a GET request. You probably intend to attach it to a base URI. May as well do both at the same time:

    You could use a URI object:

    use URI; my %params = ( name => 'josh', age => 26 ); my $uri = URI->new('http://www.example.com'); $uri->query_form(%params); print "$uri\n"; __END__ __OUTPUT__ http://www.example.com?name=josh&age=26

    Or you could construct the query param list without attaching it to a base URI by using URI::QueryParam like this:

    use URI::QueryParam; use URI; my %params = ( name => 'josh', age => 26 ); my $u = URI->new; $u->query_param($_, $params{$_}) for keys %params; print $u->query, "\n"; __END__ __OUTPUT__ name=josh&age=26

    Either construct handles proper escaping of embedded metacharacters for you automatically. And both of these examples use components of the same URI distribution, on CPAN.


    Dave

Re: url escape
by MidLifeXis (Monsignor) on Apr 22, 2015 at 12:09 UTC

    The documentation for URI::Escape says it expects a string, not a parameter list. Its purpose is to percent encode reserved characters.

    perl -MURI::Escape -e 'print uri_escape("10% is enough\n"), "\n"'

    --MidLifeXis

Re: url escape
by roboticus (Chancellor) on Apr 22, 2015 at 12:07 UTC

    bugup401:

    If you'd check the docs you'd see that it doesn't take an arbitrary structure as an argument, it wants a string. So you might try giving it the string you want escaped, and see if that works for you.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: url escape
by marto (Cardinal) on Apr 22, 2015 at 12:13 UTC

    "#output: name=josh&age=26"

    I get

    name

    Take another look at the documentation, uri_escape expects a string. Had you provided:

    my $str = '(name=>"josh",age=>26)'; print uri_escape( $str );

    The output would be:

    %28name%3D%3E%22josh%22%2Cage%3D%3E26%29

      cool, i got it bt why the output is %28name%3D%3E%22josh%22%2Cage%3D%3E26%29

      why not name=josh&age=26

        Because that is what you asked for. %28 is (, %29 is ). If you do not do so, &age= is illegal in HTML. You'd want &age= there.


        Enjoy, Have FUN! H.Merijn
        A reply falls below the community's threshold of quality. You may see it by logging in.
Re: url escape
by Anonymous Monk on Apr 22, 2015 at 23:04 UTC
    use CGI; print CGI->new( { name=>"josh",age=>26 } )->query_string; __END__ name=josh;age=26
Re: url escape
by Jenda (Abbot) on Apr 25, 2015 at 09:47 UTC
    use CGI::Enurl; print enurl ({name=>"josh",age=>26});

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.