This is a quick and untested example of how to use CGI in your script. There is alot more that it can do for you also.
#!/usr/bin/perl use strict; use CGI; my $q = new CGI; #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 = $q->param('action'); $userid = $q->param('userid'); $name1 = $q->param('name1'); $name2 = $q->param('name2'); $line = $q->param('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 { print $q->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 { print $q->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 { print $q->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"; print $q->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;


--BigJoe

Learn patience, you must.
Young PerlMonk, craves Not these things.
Use the source Luke.

In reply to Re: Re: Add,Insert,Change,Delete File / Array by BigJoe
in thread 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.