Solved

perl File management

Posted on 2002-04-14
1
327 Views
Last Modified: 2010-03-05
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
0
Comment
Question by:d2player
1 Comment
 
LVL 2

Accepted Solution

by:
hawkers earned 11 total points
ID: 6939773
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question