perl File management

File management
Ok ... i have made a simple perl script that post the informations it gets from an webpage ...
the code:
#!/usr/local/bin/perl


&GetFormInput;

$url = $field{'url'} ;
$spatiu = $field{'spatiu'} ;
$upploaad = $field{'upploaad'} ;
$ads = $field{'ads'} ;
$adresa = $field{'adresa'} ;
$Submit = $field{'Submit'} ;

open (LOGFILE, ">> server.txt") ;
print LOGFILE " <table border=0 width=750 bgcolor=#000000 cellpadding=1 cellspacing=1> <td width=22 bgcolor=#FFFFFF> <a href=$url target=_blank> $url </a> </td> <td width=24% bgcolor=#FFFFFF><div align=center>$spatiu</div></td> <td width=19% bgcolor=#FFFFFF><div align=center>$upploaad</div></td> <td width=15% bgcolor=#FFFFFF><div align=center>$ads</div></td><td width=20% bgcolor=#FFFFFF><div align=center>$adresa</div></td> </table> " ;

close (LOGFILE) ;

#flock(LOGFILE, 8) ; #UNLOCK - uncomment for locking on a Unix server.


print "Location: http://localhost/docs/lucru/\nURI: http://localhost/docs/lucru/\n\n" ;


sub GetFormInput {

(*fval) = @_ if @_ ;

local ($buf);
if ($ENV{'REQUEST_METHOD'} eq 'POST') {
read(STDIN,$buf,$ENV{'CONTENT_LENGTH'});
}
else {
$buf=$ENV{'QUERY_STRING'};
}
if ($buf eq "") {
return 0 ;
}
else {
@fval=split(/&/,$buf);
foreach $i (0 .. $#fval){
($name,$val)=split (/=/,$fval[$i],2);
$val=~tr/+/ /;
$val=~ s/%(..)/pack("c",hex($1))/ge;
$name=~tr/+/ /;
$name=~ s/%(..)/pack("c",hex($1))/ge;

if (!defined($field{$name})) {
$field{$name}=$val;
}
else {
$field{$name} .= ",$val";

#if you want multi-selects to goto into an array change to:
#$field{$name} .= "\0$val";
}


}
}
return 1;
}


all ok ...
the result is posting the information in an text file....Now the question ! If i want to modify or to delete an record from the text file ... how do i do this ?

Thanks
d2playerAsked:
Who is Participating?
 
hawkersCommented:
d2player

Here is some code I managed to modified from the DBI Programming Book for Perl. www.amazon.co.uk / www.amazon.com.


First a bit about the code.

This function  provides the facility to delete information from a temporary shopping cart folder using the following method.

1) Open up the file you would like to delete records from.
2) Create a temporary file.
3) Complete some pattern matching.(In my case I am matching the ID numbers not being passed as parameters)
4) All the unmatched ID numbers and other data is then passed through to the temp file.
5) When this process has finished I then rename the temporary file with the existing file name.
6) Delete the new temporary file with all of unwanted data and records.



Now for the code!

-----------------------------------------------------------

sub DeleteFromLog()
{
     my $fileh = $_[0];
     my $path = $_[1];
     my $cart = $_[2];
     my $temp = $_[3];
     my $query = $_[4];

     $query = substr($query,4,4);

     local (*FOLDER);
     #Open the cart log file
     open($fileh, "$path$cart")|| die "Cannot open $path$cart\n";

     if (!&OpenFolder(FOLDER, "$path"))
               {
                    #print "Error :: Cannot of the folder";
                    $errstr = "Cannot open the folder";
                    &ErrorPage($errstr);
               }
     else
               {
                   
                    my $index = 0;
                   
                    open(TEMP,">"."$path$temp") ||  die "Cannot open $path$temp\n";
                   
         
                    #While the cart log file is open
                    while (<$fileh>)
                    {
                         #Identify the values, these variables may need to be amended
                         #depends upon the  number of values set with in the file.
                         my ($id,$cat,$col,$height,$width,$quanmeas,$measure,$tp,$rp,$sup,$pic,$q) = split(/:/, $_);
                         
                         $len = length($id);

                         $id = substr($id,$len -1 ,$len - 1);
                         
                         #If the comparison  is matched then move and test the next row.
                         #Any values left in the file will be deleted.
                         if ($query eq $id)
                         {
                              next;
                         }
                         
                         print TEMP $_
                         
                         
                         
                    }
               }

     #Close the cart log file
     close($fileh);
     #Close the temporary delete bin
     close(TEMP);
     #Delete the cart log file    
     unlink ("$path$cart");
     #Rename the delete bin file with the cart log file.
     #This will give the impression that the file has been updated.
     rename ("$path$temp", "$path$cart");
}

-----------------------------------------------------------
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.