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


In reply to Re: Charecters problems by tachyon
in thread Problems encoding characters for web by Yossi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.