These are the codes used by simple_escape & unescape methods in CGI::Util module:
Code:
sub simple_escape {
return unless defined(my $toencode = shift);
$toencode =~ s{&}{&}gso;
$toencode =~ s{<}{<}gso;
$toencode =~ s{>}{>}gso;
$toencode =~ s{\"}{"}gso;
# Doesn't work. Can't work. forget it.
# $toencode =~ s{\x8b}{‹}gso;
# $toencode =~ s{\x9b}{›}gso;
$toencode;
}
# unescape URL-encoded data
sub unescape {
shift() if @_ > 1 and (ref($_[0]) || (defined $_[1] && $_[0] eq $CGI::DefaultClass));
my $todecode = shift;
return undef unless defined($todecode);
$todecode =~ tr/+/ /; # pluses become spaces
$EBCDIC = "\t" ne "\011";
if ($EBCDIC) {
$todecode =~ s/%([0-9a-fA-F]{2})/chr $A2E[hex($1)]/ge;
} else {
$todecode =~ s/%(?:([0-9a-fA-F]{2})|u([0-9a-fA-F]{4}))/
defined($1)? chr hex($1) : utf8_chr(hex($2))/ge;
}
return $todecode;
}
As you see simple_escape() function is pretty same like the one used by Bartb.
The unescape() method has also nothing special. It does additionally support the EBCDIC, utf8 encodings, but nothing special.
The only codes which is needed to compare is:
Code:
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/seg;
and
$todecode =~ tr/+/ /; # pluses become spaces
$todecode =~ s/%(?:([0-9a-fA-F]{2})|u([0-9a-fA-F]{4}))/
The only thing, which might missing from original decoding code is the + sign translation.
So the following code should be right:
Code:
$value =~ tr/+/ /; # pluses become spaces
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/seg;
Again.
Why to use the whole CGI.pm module, which does gives you a lot overhead, when you can do with a pretty short code the same thing???
Using the CGI::Util would be even better, as only this module was affected and not the whole CGI module.
But still unnecessary, when a regexp solves the problem...
EDIT: in general, I think we aggree, that CGI module is good for beginners. They save a lot programming with it. But when performance becomes important, the CGI module is not useful anymore.
Additionally IMHO, when you are depending on a module like CGI, and later if you want to ignore the usage of it because of performance reasons, it will likely need a lot additional work to replace all CGI related occurences. I already faced this situation, and I can say you, it took a lot development time to completely ignore CGI usage.
Best regards,
Webmaster33
Paid Support from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...