urlencode.pl 405 B

12345678910111213141516
  1. #!/usr/bin/perl
  2. sub UrlEncode {
  3. my @letters = split(//,shift);
  4. my @safe = ('a' .. 'z', 'A' .. 'Z', '0' .. '9', '-', '_', '.', '!', '~', '*', "'", '(', ')');
  5. foreach my $letter (@letters) {
  6. my $pattern = quotemeta($letter);
  7. if (not grep(/$pattern/, @safe)) {
  8. $letter = sprintf("%%%02x", ord($letter));
  9. }
  10. }
  11. return join('', @letters);
  12. }
  13. print UrlEncode(join(' ', @ARGV)), "\n";