Gossamer Forum
Home : General : Perl Programming :

Re: [mkp] cgi-question: html->perl text input

Quote Reply
Re: [mkp] cgi-question: html->perl text input In reply to
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{<}{&lt;}gso;
$toencode =~ s{>}{&gt;}gso;
$toencode =~ s{\"}{&quot;}gso;
# Doesn't work. Can't work. forget it.
# $toencode =~ s{\x8b}{&#139;}gso;
# $toencode =~ s{\x9b}{&#155;}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...

Last edited by:

webmaster33: Mar 2, 2006, 3:57 AM
Subject Author Views Date
Thread cgi-question: html->perl text input bartb 9605 Jan 6, 2006, 8:10 AM
Thread Re: [bartb] cgi-question: html->perl text input
webmaster33 9474 Jan 6, 2006, 8:15 AM
Thread Re: [webmaster33] cgi-question: html->perl text input
bartb 9465 Jan 6, 2006, 8:37 AM
Post Re: [bartb] cgi-question: html->perl text input
bartb 9507 Jan 6, 2006, 9:02 AM
Thread Re: [bartb] cgi-question: html->perl text input
webmaster33 9452 Jan 6, 2006, 9:54 AM
Thread Re: [webmaster33] cgi-question: html->perl text input
bartb 9462 Jan 6, 2006, 10:03 AM
Thread Re: [bartb] cgi-question: html->perl text input
webmaster33 9490 Jan 6, 2006, 10:15 AM
Post Re: [webmaster33] cgi-question: html->perl text input
bartb 9476 Jan 6, 2006, 10:22 AM
Thread Re: [webmaster33] cgi-question: html->perl text input
mkp 9398 Feb 26, 2006, 6:41 AM
Thread Re: [mkp] cgi-question: html->perl text input
webmaster33 9386 Feb 26, 2006, 7:33 PM
Thread Re: [webmaster33] cgi-question: html->perl text input
mkp 9359 Feb 26, 2006, 9:19 PM
Post Re: [mkp] cgi-question: html->perl text input
webmaster33 9343 Mar 2, 2006, 3:48 AM