Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Storing multi-line form data in a flat file?

by SilverB1rd (Scribe)
on Mar 15, 2001 at 01:47 UTC ( #64511=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on Storing multi-line form data in a flat file?

Replies are listed 'Best First'.
Re: Storing multi-line form data in a flat file?
by Hot Pastrami (Monk) on Mar 15, 2001 at 02:04 UTC
    It's difficult to know what exactly you mean with such limited information, but I think that you probably want something along these lines:
    use CGI qw(:cgi); use strict; my $flatfile = "somefile.txt"; my $query = CGI->new(); my $text = $query->param("text_box"); open(FLATFILE, ">>somefile.txt") or die "Open failed, $!"; print FLATFILE "$text\n\n"; close(FLATFILE); print "Content-type: text/html\n\n"; print "Success!";
    This code assumes that A. you want to append the data to a growing file with each submission, and B. you only want the data from a single form field called "text_box".

    Update: Oh, and it also assumes that you want 2 line breaks between each entry in the text file.

    Hot Pastrami
      Yeah that is close but what if some one typed in "haha\n\nhaha...a hundred or so later..\n\n" This could really mess up my flat file. I want to encode this so I can store the data and retrieve it later.

      ------
      The Price of Freedom is Eternal Vigilance
        If the user types in a backslash than an "n", you'll be fine. If he/she puts in line breaks you simply wish to get rid of, use this:
        $text =~ tr/\n\r//;
        If you want to replace the line breaks with some other character, place the replacement character between the 2nd and 3rd slashes.

        Update: An easier way might be to borrow escape() from the CGI module, which escapes anything non-alphanumeric to it's alias (like "&" becomes "&"):
        $text = CGI::escape($text);
        When you want to translate it back to it's original form, use CGI::unescape();

        Or, if you're trying to make a home-grown database (which is not recommended), try using Berkeley DBM. It'll take care of all that stuff for you, be much faster, and much less bug-prone.

        Hot Pastrami
Re: Storing multi-line form data in a flat file?
by bbfu (Curate) on Mar 15, 2001 at 02:22 UTC

    Why not just use URI encoding? It should work for most cases, though it's probably not 100%

    I think CGI also has an escape or encode method... Update: Actually, looks like CGI just does URI encoding, too. But I don't see a method mentioned in the docs (just a quick check) that will let you do it. URI::Escape is probably the way to go.

    bbfu
    Seasons don't fear The Reaper.
    Nor do the wind, the sun, and the rain.
    We can be like they are.

      Thanks URI encoding looks like it will work.

      ------
      The Price of Freedom is Eternal Vigilance
Re: Storing multi-line form data in a flat file?
by SilverB1rd (Scribe) on Mar 15, 2001 at 02:01 UTC
    Ok I make the form field with
    use CGI;<br> my ($q) = new CGI;<br> print $q->textarea('description','',20,50);<br>
    then when the form comes back I need to encode q$->param('description'); some how so I can store it.

    All I need to know is how to encode 'description' so that special characters wont do funny things to my flat files. I also want to display this information the way it was submitted so I cant just remove the them.

    ------
    The Price of Freedom is Eternal Vigilance
Re: Storing multi-line form data in a flat file?
by arturo (Vicar) on Mar 15, 2001 at 02:25 UTC

    Hmm, here's my thought: store each line of the multiline data (sans newline) in an array, and serialize yon array before you store it in the flat file. Storable or FreezeThaw are worth checking into here.

    Other solution: why use flatfiles?

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Re: Storing multi-line form data in a flat file?
by Masem (Monsignor) on Mar 15, 2001 at 02:11 UTC
    Unfortunately, there's always going to be a small cavaet to doing this, but if you can assume that a certain symbol or group of symbols will not be used by those entering the form, you can substitute that symbol(s) in for the carriage return. For example:
    my $replacement = "##"; $multiline_data =~ s/\n\r?/$replacement/g; print <OUTFILE> $multiline_data; ... my $read_data = <INFILE>; $read_data =~ s/$replacement/\n/g;
    The problem being that if someone used '##' in their string, that would be replaced by \n on the return loop.

    A better option is that you store the multiline data into it's own file separate from the flat file, the filename related to an id or the like. This way, you preserve the \n and don't have a problem with losing someone's text.


    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
      Thats why I want to encode the data so I can preserve the coding.

      ------
      The Price of Freedom is Eternal Vigilance
Re: Storing multi-line form data in a flat file?
by Tuna (Friar) on Mar 15, 2001 at 01:49 UTC
    It would help us to help you if you: a. provided an example of your data and b. provided an example of code.
Re: Storing multi-line form data in a flat file?
by extremely (Priest) on Mar 16, 2001 at 02:13 UTC
    One way you do this is pick two special characters. Pick ones that aren't likely to be used often like "\030" and "\031". Now do this:
    my %codes = ("\031" => "\030\001", "\031" => "\030\001"); foreach $item (@items) { $item =~s/(\031|\030)/$codes{$1)/ge; print FILE $item."\031"; }

    Now the only "\030"s that appear in the file are the record seperators and you do a simple match like that one to restore the occasional encoded values.

    --
    $you = new YOU;
    honk() if $you->love(perl)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://64511]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2022-08-08 19:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?