Yossi has asked for the wisdom of the Perl Monks concerning the following question:

i made a really simple weblog (all i need to do is design it) but i have a problem with hebrew charecters and charecters like ! or @ or % - all of that... that make them a number with %, for example, ! = %21. my code is:
#! /usr/bin/perl $in; if ($ENV{'REQUEST_METHOD'} eq "get") { $in = $ENV{'QUERY_STRING'}; } else { $in = <STDIN>; } $in =~ s/\+/ /g; @detail = split (/&/, $in); print "Content-Type: text/html\n\n"; print "<html><body>Post has succsesfully submited"; foreach $details(@detail) { %details = split (/=/, $details); while (($name, $value) = each %details) { print "<br>\n"; $entry .= $value . "#"; } } print "click <a href=view.cgi>here</a> to view your post</body></html> +"; $entry = $entry."\n"; open (GBOOK, ">> guestbook.txt"); print GBOOK "$entry"; close (GBOOK);
that was the submit code (the "print"s are transleted to english) the view code is:
#! /usr/bin/perl open (GBOOK, "guestbook.txt"); @entries = <GBOOK>; close (GBOOK); print "Content-Type: text/html\n\n"; print "<html><body>"; foreach $entry(@entries) { @singles = split (/#/, $entry); print "$singles[0]\n"; print "<BR><BR>\n"; } print "</body></html>";
an online failure at http://dotkof.org/yossi

Edit of title by tye

Replies are listed 'Best First'.
Re: Charecters problems
by tachyon (Chancellor) on Jul 22, 2001 at 05:11 UTC

    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

      10x!!!!! i will try to do this tomarrow - now it's 6:15 AM in israel - and i didn't go to sleep yet...