http://www.perlmonks.org?node_id=116696


in reply to Add,Insert,Change,Delete File / Array

Hey, you are going to want to check out a fun place we like to call CPAN. There you will find cool modules like CGI.pm and DBI which will help you on your website quests.

With DBI and it's drivers you can connect to almost any Database (Even CSV files!). Go to http://search.cpan.org and search for "DBI" and it will give you all sorts of tidbits of things you can do. While you are there you may wish to look at CGI.pm. This does all you IO better.

--BigJoe

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

UPDATE ALWAYS have: use strict;
  • Comment on Re: Add,Insert,Change,Delete File / Array

Replies are listed 'Best First'.
Re: Re: Add,Insert,Change,Delete File / Array
by davorg (Chancellor) on Oct 04, 2001 at 17:47 UTC

    No need to go to CPAN for CGI.pm. It's been a part of the standard Perl distribution for many years.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you don't talk about Perl club."

      Thathom can at least look at the POD and then it makes him aware of where to find information about more modules that are there for using.

      --BigJoe

      Learn patience, you must.
      Young PerlMonk, craves Not these things.
      Use the source Luke.
Re: Re: Add,Insert,Change,Delete File / Array
by BigJoe (Curate) on Oct 04, 2001 at 17:51 UTC
    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.