perl File management

Posted on 2002-04-14
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:


$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);
else {
if ($buf eq "") {
return 0 ;
else {
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})) {
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 ?

Question by:d2player
1 Comment

Accepted Solution

hawkers earned 11 total points
ID: 6939773

Here is some code I managed to modified from the DBI Programming Book for Perl. /

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";
                    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)
                         print TEMP $_

     #Close the cart log file
     #Close the temporary delete bin
     #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");


Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hard perl script 16 157
compress files in RAR using perl 13 81
batch script for automated email 12 111
XPath Syntax help in conjunction with perl XML::LibXML 18 113
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit If you want to manage em…

778 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