Here's an example showing what you want to do with DBD::CSV:

#!/usr/bin/perl -w #Define the behaviour of DBI->connect for the entire script $ENV{DBI_DSN} ||= 'DBI:CSV(RaiseError=>1,AutoCommit=>1):'. join ';', "csv_eol=\n", 'csv_sep_char=|', 'csv_quote_char=', 'csv_escape_char='; use strict; use DBI; use CGI; my $statement = q{ UPDATE list SET title = ? , description = ? WHERE id = ? }; my $cgi = CGI->new; #Connect to the data source my $dbh = DBI->connect; $dbh->do( $statement, {}, $cgi->param('title'), $cgi->param('description'), $cgi->param('id'), ); $dbh->disconnect; print $cgi->header, 'Updated List'; __END__

Note: The above code assumes that your file name is "list". DBD::CSV can only process files without any extension. Also, if this is going into production you might want turn on Taint checking by appending a T to the first line, as well as validating each of the CGI paramters, possibly using HTML::FormValidator, which is my personal favorite for this.

The great thing about using DBD::CSV is that if you ever switch to using a relational database all the code won't need to change, assuming your schema stays the same. This is why I explicitly define the environment variable $ENV{DBI_DSN} at the top of the script, so that you can easily change it later. Keep in mind this varible globally defines the data source that all the DBI->connect method calls will use, and can be globally set for all your CGI's in Apache's httpd.conf.

As well, I do suggest you look into relational databases, like MySQL or PostgreSQL. They might be better suited for storing this type of information than a flat file.


In reply to (dkubb) Re: (2) Using Files w/DBD::CSV by dkubb
in thread Using Files by Anonymous Monk

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.