OK,
Two methods:
- Use the Perl Data Base Interface (DBI) module
This is a Perl module that lets you write database code in standard SQL regardlesss of the actual database you are using, including flat-files. You'll pretend you're using an actual rdbms, write your code in sql, and DBI will take care of translating that into the commands that will interface with your flat-file. A good introduction to the Perl DBI is http://www.perl.com/pub/a/1999/10/DBI.html. Then, if you decide later on to switch to a real db, you'll have just a few lines of your code to change as you tell DBI which type of db to interface with, instead of the whole thing to re-write. All this work on DBI's part does come at a price - it adds a layer of interpretation and slows things down a bit. But you won't notice it witha flat-file until your user list approaches 1,000 or so rows.
- Raw Perl. Here you go:
Suppose your user "database" is a flat-file that looks like this:
username|password|fname|lname|user_info
Your html form in which you select the user to delete is going to have a drop down:
<form action="myscript.pl">
<select name="user">
<OPTION VALUE="uname_1">user name 1
<OPTION VALUE="uname_2">user name 2
</select>
</form>
Then your Perl script is going to look like:
# use the CGI module, this automates processing of form input and take
+s care of a lot of security issues too
use CGI;
my $query = new CGI;
$user_to_delete = $query->param('user');
#load the user file into an array
open(FILE, '<user_data_file.txt');
@user_data = <FILE>;
close(FILE);
#to find the row with your user, cycle through the rows, parsing each
+one and looking for the user name, and write all lines back to the us
+er database except the one for the user in question
open(FILE, '>user_data_file.txt');
for $i (0 .. $#user_data) {
@this_user_info = split(/\|/, $user_data[$i]);
if ($this_user_info[0] eq $user_to_delete) {
#do nothing
}
else {
print FILE "$user_data[$i]\n";
}
}
close(FILE);
This is very basic. You should add code for file locking, for example. And this could could be more elegant with the use of "unless" instead of the if-else.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.