in reply to Re: incorrect use of URI::Escape?
in thread incorrect use of URI::Escape?

You can use a closure for efficiency like this:

{ # use a closure to make a private memory for sub. my %escapes; sub uri_escape { my($text) = @_; # the first time the sub is called %escapes is undef # so we build a char->hex map. because we use a closure # %escapes survives from one calling of this function # to the next so we only map this once %escapes || do{ $escapes{chr $_} = sprintf("%%%02X", $_) for 0 +..255 }; return undef unless defined $text; $text =~ s/([^A-Za-z0-9\-_.!~*'()])/$escapes{$1}/g; return $text; } }

Update

Updated per Juerd's comments.

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Replies are listed 'Best First'.
Re: Re: Re: incorrect use of URI::Escape?
by Juerd (Abbot) on Apr 13, 2002 at 18:27 UTC

    [^;\/?:@&=+\$,A-Za-z0-9\-_.!~*'()]

    You probably do not want to exclude ;, &, = and +.

    Although they're don't have to be encoded, according to the rfc, ; and & separate key/value pairs in a query string, = separates key and value in a key/value pair in a query string, and + often is a space (chr(32)) in query strings. Because query strings are a very common reason for using URI encoding, I think it's unwise to not encode these characters.

    The characters I mentioned are part of the "reserved" characters, and since version 1.16, URI::Escape does encode them (not encoding them cause a LOT of trouble in many situations).

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.
    

      The code I offered is exactly the same as uri_escape() simply because if you are going to suggest an alternative it seems logical to KISS. I agree with you that URI:Escape is not optimal and always roll my own. Like you I do not exclude ; & + and = (also ?)

      I think a good working knowledge of URI encodings is an important thing for anyone who works with CGI

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

        The code I offered is exactly the same as uri_escape()

        An old version, apparently. I advise you to upgrade URI::Escape to version 1.16 or later, and update all of your scripts that do not encode reserved characters, or at least your post ;)

        Just FYI: I peeked at URI::Escape's source too, but I already have the new version...

        Update s/advice/advise/ per crazyinsomniac's advice to wear a hat.

        - Yes, I reinvent wheels.
        - Spam: Visit eurotraQ.