in reply to Problems encoding characters for web

The encoding/decoding of HTTP encoded strings can be done with these subs:

sub encode { my $encode = shift; $encode =~ s/([^\w\s.!~*()'-=&])/sprintf "%%%02X", ord($1)/eg; $encode =~ tr/ /+/; return $encode; } sub decode { my $decode = shift; $decode =~ tr/+/ /; $decode =~ s/%([a-f\d][a-f\d])/pack("c",hex($1))/ige; return $decode; } $str = qq/Arnie said, "I'll be back!"/; $enc = encode ($str); $dec = decode ($enc); print "$enc\n$dec";

Whilst this will work you would be better using CGI.pm to parse your CGI data. See use CGI or die; and No excuses about not using CGI.pm. To convert your script to CGI.pm you would do this:

#!/usr/bin/perl -w use strict; use CGI; my $query = new CGI; # get all the name/value pairs in a hash my %details = $query->Vars; my $entry = join "#", values %details; open (GBOOK, ">> guestbook.txt") or die "Oops, Perl says $!\n"; print GBOOK "$entry\n"; close GBOOK; # now print some HTML out using a herepage my $html_entry = join "<BR>\n", values %details print << HTML; Content-type: text/html <html> <head> <title>Thanks!</title> </head> <body> <h1>Thanks!</h1> <p>Here is the data stored.</p> <p>$html_entry <p>Click <a href="./view.cgi">here</a> to view your post </body> </html> HTML __END__

The beauty of CGI.pm is it handles all the details for you like the encoding. One suggestion/query - What happens if your entry data contains the # symbol you are using to separate records? The solution is to encode this symbol when writing the file and decode it after splitting on it when retrieving the data. You would encode like this:

# encode all the '#' chars before we join s/#/%35/g for values % details; # now the only '#' chars represent our delimiter my $entry = join "#", values %details; # getting the data from the file we reverse the process # first split on the '#' char to get our values my @values = split "#", <FILE>; # now unencode any encoded '#' chars in @values s/%35/#/g for @values;
</code>

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Replies are listed 'Best First'.
Thank you!
by Yossi (Initiate) on Jul 22, 2001 at 07:13 UTC
    10x!!!!! i will try to do this tomarrow - now it's 6:15 AM in israel - and i didn't go to sleep yet...