For Programmers: Free Programming Magazines  


Home > Archive > PERL Programming > October 2005 > Convert a Decoded UIRL to an Encoded









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Convert a Decoded UIRL to an Encoded
homebunny

2005-10-29, 7:55 am


Hi All,

I want a script that decodes an URL for me,

I have never written a perl script before (Only PHP an VB6)

This code snippet have I found:
sub URLDecode {
my $theURL = $_[0];
$theURL =~ tr/+/ /;
$theURL =~ s/%([a-fA-F0-9]{2,2})/chr(hex($1))/eg;
$theURL =~ s/<!--(.|\n)*-->//g;
return $theURL;
}
http://glennf.com/writing/hexadecimal.url.encoding.html

How can I get this to work?

Many thanks in advance!


Gunnar Hjalmarsson

2005-10-29, 7:55 am

homebunny wrote:
> I want a script that decodes an URL for me,
>
> I have never written a perl script before (Only PHP an VB6)
>
> This code snippet have I found:
> sub URLDecode {
> my $theURL = $_[0];
> $theURL =~ tr/+/ /;
> $theURL =~ s/%([a-fA-F0-9]{2,2})/chr(hex($1))/eg;
> $theURL =~ s/<!--(.|\n)*-->//g;
> return $theURL;
> }
> http://glennf.com/writing/hexadecimal.url.encoding.html
>
> How can I get this to work?


By using that function in a Perl program, perhaps?

But you do of course not need Perl to decode a URL. If you know PHP, why
don't you stick to it?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
homebunny

2005-10-29, 6:59 pm

Hello Gunnar,

You are right, I was thinking on perl.
The reasing is that i'm expecting a lot of hits on this redirect script (daily
>10000), and sometime ago i have read somewhere that perl has a better performance

then php (and less server load), so i thought maybe we can do this in perl
and learn something new ... But it is harder then I thought ;-)

What would be the best option to go ahead?

Many thanks,


> Gunnarwrote:
>
> By using that function in a Perl program, perhaps?
>
> But you do of course not need Perl to decode a URL. If you know PHP,
> why don't you stick to it?
>



Gunnar Hjalmarsson

2005-10-29, 6:59 pm

homebunny wrote:
> You are right, I was thinking on perl.
> The reasing is that i'm expecting a lot of hits on this redirect script
> (daily > 10000), and sometime ago i have read somewhere that perl has a better
> performance then php (and less server load), so i thought maybe we can do this in
> perl and learn something new ...


Learning a new programming language for such a trivial thing, just
because you "read somewhere" about differences in performance, seems to
be overkill.

> But it is harder then I thought ;-)


Learning Perl isn't hard, and it's fun, too.

http://learn.perl.org/

> What would be the best option to go ahead?


Learn Perl if you want to learn Perl.

Otherwise, stick to the programming languages you know. Which language
you use will not make a big difference to your server even if it's
10,000 hits.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
homebunny

2005-10-29, 6:59 pm

Hello Gunnar,

> Learning Perl isn't hard, and it's fun, too.
>
> http://learn.perl.org/
>


I love it already, it wasn't that hard, luckily i founded some code examples
;-)

http://glennf.com/writing/hexadecimal.url.encoding.html
http://www.webreference.com/perl/tutorial/7/index.html

Many thanks !!


BTW, This works perfect for me:

#!/usr/bin/perl -w
# redirect.pl - redirects a browser to the URL contained in QUERY_STRING
# by Jonathan Eisenzopf. v1.0 19990818
# Copyright (c) 1999 Jupitermedia Corp. All Rights Reserved.
# See http://www.webreference.com/perl for more information
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

#
# Downloaded at : http://www.webreference.com/perl/tutorial/7/index.html
#
# Modified By Jimmy Kerckhofs - Oct 2005
#
# Removed: HTTP referer, restriction on referer
# Added: Decode URL as Output
# http://glennf.com/writing/hexadecimal.url.encoding.html
#

use strict;
use CGI;
use CGI::Carp;
use URI;

# MAIN
# my $q = CGI->new;
my $q = new CGI;

# print error if QUERY_STRING is empty
&error("Must specify URL") unless defined $q->keywords;

# Convert URL
&URLDecode($q->keywords);

# redirect
print $q->redirect($q->keywords);

# SUBROUTINES
sub error {
my $message = shift;
print $q->header;
print "<H1>$message</H1>";
die "$message";
}

sub URLDecode {
my $theURL = $_[0];
$theURL =~ tr/+/ /;
$theURL =~ s/%([a-fA-F0-9]{2,2})/chr(hex($1))/eg;
$theURL =~ s/<!--(.|\n)*-->//g;
return $theURL;
}


Gunnar Hjalmarsson

2005-10-29, 6:59 pm

homebunny wrote:
> BTW, This works perfect for me:
>
> #!/usr/bin/perl -w
> # redirect.pl - redirects a browser to the URL contained in QUERY_STRING
> # by Jonathan Eisenzopf. v1.0 19990818
> # Copyright (c) 1999 Jupitermedia Corp. All Rights Reserved.
> # See http://www.webreference.com/perl for more information
> #
> # This program is free software; you can redistribute it and/or modify
> # it under the terms of the GNU General Public License as published by
> # the Free Software Foundation; either version 2 of the License, or
> # (at your option) any later version.
> # # Downloaded at : http://www.webreference.com/perl/tutorial/7/index.html
> # # Modified By Jimmy Kerckhofs - Oct 2005
> # # Removed: HTTP referer, restriction on referer
> # Added: Decode URL as Output
> # http://glennf.com/writing/hexadecimal.url.encoding.html
> #
> use strict;
> use CGI;
> use CGI::Carp;
> use URI;
>
> # MAIN
> # my $q = CGI->new;
> my $q = new CGI;
>
> # print error if QUERY_STRING is empty
> &error("Must specify URL") unless defined $q->keywords;
>
> # Convert URL
> &URLDecode($q->keywords);
>
> # redirect
> print $q->redirect($q->keywords);
>
> # SUBROUTINES
> sub error {
> my $message = shift;
> print $q->header;
> print "<H1>$message</H1>";
> die "$message";
> }
>
> sub URLDecode {
> my $theURL = $_[0];
> $theURL =~ tr/+/ /;
> $theURL =~ s/%([a-fA-F0-9]{2,2})/chr(hex($1))/eg;
> $theURL =~ s/<!--(.|\n)*-->//g;
> return $theURL;
> }


My god! You said in a previous message that you wanted to use Perl
instead of PHP because you were worried about the server load. Assuming
that the above script is run as a plain CGI script, I'm pretty sure that
it will cause a lot more server load than would have been the case if
you had used PHP, especially since it makes use of the heavy-weight
CGI.pm module.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com