Hi there, I have written this code. It all works and allows me to Add,insert,change,delete and re-edit records in a file. I want to create a small database for my personal site. Mostly to store stories,artwork etc. Does this look ok? Are there any problems here regarding people adding at the same time? or loosing the data whilst manipulating it via an array? Could do with some wise opinions. To run the script, copy it. Give it a name, change the 3 lines of code and create a datafile.
#!/usr/bin/perl #SPLIT INCOMING DATA ################################################################### read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); $buffer=$ENV{QUERY_STRING} if ($ENV{QUERY_STRING}=~/=/); @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; if ($F{$name}) { $F{$name} = $F{$name}.",".$value; } else { $F{$name} = $value; } } #VARIABLES TO CHANGE ############################ $scriptfile = 'test.cgi'; #Name of this script $datafile = 'testdata.dat'; #Name of the file to sto +re data to $workdir = 'http://myworld.server101.com'; #Default Working Dir #OTHER VARIABLES ############################ $action = $F{'action'}; $userid = $F{'userid'}; $name1 = $F{'name1'}; $name2 = $F{'name2'}; $line = $F{'line'}; #DECIDE ACTION TO TAKE ############################ if ($action eq '') { &Show;} if ($action eq 'show') { &Show;} if ($action eq 'add') { &Add;} if ($action eq 'addsave') { &Addsave;} if ($action eq 'change') { &Change;} if ($action eq 'changesave'){ &Changesave;} if ($action eq 'insert') { &Insert;} if ($action eq 'insertsave'){ &Insertsave;} if ($action eq 'delete') { &Delete;} #ADD - Create Form ############################ sub Add { &Header; print qq| <form action=$scriptfile> <input type=hidden name=action value=addsave> ForeName:<input type=text name=name1 size=20><br> Surname :<input type=text name=name2 size=20><br> <input type=submit value="Add Record"> </form> |; exit; } #ADD - Save data to File ############################ sub Addsave { $newname = join('¬', $name1, $name2); $newname .= "\n"; open ("database", ">>$datafile") || &Printerror; print database $newname; close(database); print "Location: $workdir/cgi/$scriptfile?action=show\n\n"; exit; } #CHANGE - Create Form ############################ sub Change { &Header; open ("database", "$datafile") || &Printerror; @database = <database>; close(database); ($name1, $name2) = split ('[¬]',@database[$line]); $name2 =~ s/\n//g; print qq| <form action=$scriptfile> <input type=hidden name=action value=changesave> <input type=hidden name=line value=$line> Forename:<input type=text name=name1 size=20 value=$name1><br> Surname :<input type=text name=name2 size=20 value=$name2><br> <input type=submit value="Change Record"> </form> |; exit; } #CHANGE - Save changed data to File ################################### sub Changesave { $newname = join('¬', $name1, $name2); $newname .= "\n"; open ("oldbase", "$datafile") || &Printerror; @oldbase = <oldbase>; close(oldbase); @oldbase[$line] = $newname; open ("newbase", ">$datafile") || &Printerror; print newbase @oldbase; close(newbase); print "Location: $workdir/cgi/$scriptfile?action=show\n\n"; exit; } #INSERT - Create Form ############################ sub Insert { &Header; print qq| <form action=$scriptfile> <input type=hidden name=action value=insertsave> <input type=hidden name=line value=$line> Forename:<input type=text name=name1 size=20><br> Surname :<input type=text name=name2 size=20><br> <input type=submit value="Insert Record"> </form> |; exit; } #INSERT - Save inserted data to File ##################################### sub Insertsave { $newname = join('¬', $name1, $name2); $newname .= "\n"; open ("oldbase", "$datafile") || &Printerror; @oldbase = <oldbase>; close(oldbase); $oldname = @oldbase[$line]; @oldbase[$line] = "$newname$oldname"; open ("newbase", ">$datafile") || &Printerror; print newbase @oldbase; close(newbase); print "Location: $workdir/cgi/$scriptfile?action=show\n\n"; exit; } #DELETE - delete record ################################### sub Delete { open ("oldbase", "$datafile") || &Printerror; @oldbase = <oldbase>; close(oldbase); @oldbase[$line] = ''; open ("newbase", ">$datafile") || &Printerror; print newbase @oldbase; close(newbase); print "Location: $workdir/cgi/$scriptfile?action=show\n\n"; exit; } #SHOW - Create List of Records ############################## sub Show { $line = "-1"; &Header; open ("database","$datafile")|| &Printerror; @database = <database>; close(database); foreach (@database) { $line += 1; ($name1, $name2) = split ('[¬]',$_); print qq| $line $name1 $name2 \| <a href=$scriptfile?action=change&line=$line>Edit</a> \| <a href=$scriptfile?action=delete&line=$line>Delete</a> \| <a href=$scriptfile?action=insert&line=$line>Insert</a><br>|; } print qq| <br> <a href=$scriptfile?action=add>Add Record</a> |; exit; } #HEADER ##################################### sub Header { print "Content-type: text/html\n\n"; }
ThAtH0M

In reply to Add,Insert,Change,Delete File / Array by Thathom

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.