File Operations in Perl

Open

open(FILEHANDLE,”filename.txt”)

Opens the file specified for read (update not allowed)

Example of open:

$db="/www/emailadd.txt";
open (DB,"$db") || die "Can't Open mailadd: $!\n";
while (<DB>) {
   chop;
  &SendMail;
}
close DB;

open(FILEHANDLE,”>filename.txt”)

Opens tthe file specified for write. This function destroys the existing file by writing a file of the same name

open(FILEHANDLE,”>>filename.txt”)

Add records to the file specified.

open(FILEHANDLE,”+<filename.txt”)

Before I got serious about using MYSQL to handle database accesses, I used this heavily. Safely lets you update a text file on a server. Here is an example of the type of code I was writing before I moved to MYSQL

open FH, "+</mnt/web/guide/www/lodge.txt" or die "Cannot open $file: $!";
flock FH, 2;
seek FH, 0, 0;
my @content = <FH>;
foreach $item (@content){
(@warray)=SplitTabArg($item);
$file{$warray[0]} = $item;
} #foreach
if ($record ne ""){
$file{$FORM{'number'}} = $record;}
}
seek FH, 0, 0;
truncate $file, 0;
print FH values(%file);
close FH;

open(FILEHANDLE,”+>filename.txt”) read/write but first truncate

Return to Perl Help


[Our portfolio] [A history of Computers and Networks]
[Perl help] [ABAP help] [MySQL help]
[TCP/IP troubleshooting] [HTML help] [Feedback] [Humor]
Advertise on Golden Ink's Georgia Network


In Association with Amazon.com