http://qs321.pair.com?node_id=554654

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

I see this rather a lot on scripts I download, but I don't really understand it. Can anyone help? Thanks.

$foo =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

Replies are listed 'Best First'.
Re: common replacement code ill-understood
by davido (Cardinal) on Jun 11, 2006 at 05:45 UTC

    It matches a two-digit hexidecimal number preceded by a %. That entity ( %7F, for example) is converted to a base-10 equivalent (some number between 0 and 255), and then that number is converted into a single byte, and substituted into the original string in place of the original hexidecimal entity.

    See perlre, hex, and pack for further information.


    Dave

Re: common replacement code ill-understood
by Joost (Canon) on Jun 11, 2006 at 06:52 UTC

      You can use CGI to decode parameters, but it won't decode the main url part. CGI doesn't have a generic %-unescape routine.

        To escape and unescape URI/URL escape characters, use URI::Escape. A very handy module.


        If the information in this post is inaccurate, or just plain wrong, don't just downvote - please post explaining what's wrong.
        That way everyone learns.

Re: common replacement code ill-understood
by planetscape (Chancellor) on Jun 11, 2006 at 19:50 UTC
Re: common replacement code ill-understood
by chromatic (Archbishop) on Jun 12, 2006 at 04:40 UTC

    It means that the code probably has bugs, especially security problems.