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

Hello Monks !

I'm using CGI.pm save method to save form data in a file:

sub sparaKunddata { open (KOF, ">$konfil") or die "Kan inte \xF6ppna filen $konfil. $!"; flock (KOF, 2) or die "Kan inte l\xE5sa filen $konfil. $!"; $qry-> save (*KOF) or die "Kan inte spara filen $konfil. $!"; close KOF;}
The problem is that file is saved with URL encoded Swedish letters, even if client is set to ISO 8859-1 and header from server is set to same
print $qry->header(-type => "text/html; charset=iso-8859-1;", -cookie => $kaka);
. Is there any way to recode chars before file is saved with save() method or do I have to manipulate file after it's being saved ? Prefferably without using any non-core module

Thanks in advance !

Replies are listed 'Best First'.
Re: Save form data with decoded URL chars
by Anonymous Monk on Aug 08, 2012 at 10:19 UTC

    The problem is that file is saved with URL encoded Swedish letters, even if client is set to ISO 8859-1 and header from server is set to same

    How is that a problem ?

    save is a CGI.pm serialization method, if you want to read the file again, use CGI.pm's new, and then use param as usual, and decode / encode as usual ( perlunitut: Unicode in Perl#I/O flow (the actual 5 minute tutorial) )

    Besides that, you should start writing functions that take arguments, functions that do not operate on global variables, like I already showed you

      Hej Anonimous monk and thanks for your reply,

      The problem is that my original csv file is ISO-8859-1 encoded and that all clients that will use this script will use ISO-8859-1 on their browsers. I did this (exactly that you showed me before :-D ) :

      sub URLDekod { my $URLkodad = $_[0]; $URLkodad =~ tr/+/ /; $URLkodad =~ s/%([a-fA-F0-9]{2,2})/chr(hex($1))/eg; return $URLkodad; }
      but that means I have to open a file again, run through subroutine, save it to another file and then close and unlink and rename and so on.

      I was just looking for TMTOWTDI from more experienced people that probably is much shorter then my solution (having in mind simple fact that I have som problems by taking C out of my head).

      This is what I did after saving file

      open (TEMP, "<$tempfil") or die "Kan inte \xF6ppna filen $kontorfil"; open (NYF, "+>$nyfil") or die "Kan inte \xF6ppna filen $nyfil"; flock (NYF,2) or die $!; while (<TEMP>) { print NYF URLDekod($_); close TEMP; close NYF; }

      Any better TMTOWTDI is highly appreciated.

        but that means I have to open a file again, run through subroutine, save it to another file and then close and unlink and rename and so on.

        No you don't, why do you think you need to do that?

        Any better TMTOWTDI is highly appreciated.

        Its simple, don't use save, use param