Solved

perl File management

Posted on 2002-04-14
1
330 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
Six Sigma Control Plans

729 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