0

I have this piece of code for simple url encoding:

$enc_url= 'http://example.com/exampledir/file.pl';
$enc_url=~ s/(\W)/ '%' . sprintf('%02x',ord($1)) /ge ;

Now, it does exactly what I want but it returns the encoded url with lowercase characters

So I get:

http%3a%2f%2fwww%2eexample%2ecom%2fexampledir%2ffile%2fpl

but I want (for my own readability):

http%3A%2F%2Fwww%2Eexample%2Ecom%2Fexampledir%2Ffile%2Epl

I have tried:

$enc_url=~ s/(\W)/ uc('%' . sprintf('%02x',ord($1))) /ge ;

as well as:

$enc_url=~ s/(\W)/ '%' . uc(sprintf('%02x',ord($1))) /ge ;

and:

$enc_url=~ s/(\W)/ '%' . sprintf('%02x',uc(ord($1))) /ge ;

with no luck.

I note that a similar question for another language here (Get string from Server.UrlEncode as uppercase) has a lot of quesries on why the need to change to uppercase which I understand but I still will like to if possible/trivial to do.

Main thing is that I can not use the URI::Escape or similar modules.

Community
  • 1
  • 1
Dayo
  • 12,413
  • 5
  • 52
  • 67
  • For _readability_, don't encode periods, underscores, dashes, or tildes. These are perfectly acceptable URI characters. Your URL of `http://example.com/exampledir/file.pl` should be fine without URI encoding. – David W. Jan 04 '15 at 21:11
  • If you can't use a module, you might be able to _borrow_ it's [source code](https://metacpan.org/source/MITHUN/URI-Encode-v1.0.1/lib/URI/Encode.pm). `URI::Encode` is pretty simple and all in Perl. You can easily incorporate it into your program. – David W. Jan 04 '15 at 21:18
  • @DavidW. I know this is not what you had in mind but I was able to use the source code to change my regex from the blunt `s/(\W)/ '%' . sprintf('%02x',ord($1)) /ge` to a more refined and readable `s/([^a-zA-Z0-9\Q-_.~\E\%])/ '%' . sprintf('%02X',ord($1)) /ge`. Thanks! – Dayo Jan 04 '15 at 21:59
  • Actually, that's what I had in mind. The module `URI::Escape` is used by thousands of developers, and throughly tested through the hot fire of production. Why not learn from the authors what they had to learn through trial and error? BTW, look at [PerlBrew](http://perlbrew.pl). It's an easy way to install a user version or Perl package that's probably more _modern_ than what's on the server, and makes it simple to download modules that you need. I use it (with permission from IT) on our servers. – David W. Jan 05 '15 at 02:42

1 Answers1

1

You can use %02X instead of %02x; %X is a perl extension to sprintf that prints hex digits using uppercase letters.

Your second example (with uc(sprintf('%02x', ...))) should have worked just fine, though. Are you sure it didn't?

Finally, you should consider using URI::Escape instead of writing your own; it's more in the spirit of "saying what you mean" (everyone should know what URI escaping is) and less error-prone.

hobbs
  • 223,387
  • 19
  • 210
  • 288
  • I highly recommend to use URI::Escape. Rolling your own should be highly discouraged. – David W. Jan 04 '15 at 21:09
  • Sorted! Noted the comments here and to the question – Dayo Jan 04 '15 at 21:20
  • Even URI::Escape is probably more work than necessary. I usually use the form building capabilities of URI (sometimes with the help of URI::QueryParam) rather than using URI::Escape – ikegami Jan 04 '15 at 21:25