CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Sub routines using file locking

 



mmcw
User

Apr 30, 2003, 11:29 AM

Post #1 of 3 (183 views)
Sub routines using file locking Can't Post

I am using a couple of sub-routines using file locking.

Because I am new to locking and sysopen I want to know if this is the right way to do or can it be done better! I found most of the information at: http://www.perldoc.com/perl5.8.0/pod/perlfaq5.html#How-do-I-flush-unbuffer-an-output-filehandle---Why-must-I-do-this-


Code
   

#########################################################################
# #
# subroutine create_file #
# #
#########################################################################

sub create_file {

my ($datafile) = shift;

use strict;

open(DATA,">$datafile") or
my_die("Error in subroutine create_datafile: Can't open $datafile",$!);
close(DATA);
}

#########################################################################
# #
# subroutine check_datafile #
# #
#########################################################################

sub check_datafile {

my ($datafile,$r_variables) = @_;

use strict;

use 5.004;
use Fcntl qw(:DEFAULT :flock);

unless (-e $datafile) {
sysopen(DATA, "$datafile", O_WRONLY|O_CREAT, 0666) or die "Error in subroutine check_datafile: Can't open $datafile: $!";
flock(DATA, LOCK_EX) or die "Error in subroutine check_datafile: Can't write-lock $datafile: $!";

foreach (@{$r_variables}) {
print DATA "$_\n";
}

close(DATA);

chmod(0666,$datafile);
}

}

#########################################################################
# #
# subroutine check_dir #
# #
#########################################################################

sub check_dir {

my ($folder) = shift;

use strict;

unless (-e $folder) {
mkdir($folder,0777) or my_die("Error in subroutine check_dir: Can't make dir $folder",$!);
chmod(0777,$folder);
}
}

#########################################################################
# #
# subroutine readdata #
# #
#########################################################################

sub readdata {

my ($datafile) = shift;
my $r_data = [];

use strict;

use 5.004;
use Fcntl qw(:DEFAULT :flock);

sysopen(DATA, $datafile, O_RDONLY) or die "Error in subroutine readdata: Can't open $datafile: $!";
flock(DATA, LOCK_SH) or die "Error in subroutine readdata: Can't read-lock $datafile: $!";

@{$r_data} = <DATA>;

close(DATA);

return $r_data;
}

#########################################################################
# #
# subroutine writedata #
# #
#########################################################################

sub writedata {

my ($datafile,$r_rows) = @_;

use strict;

use 5.004;
use Fcntl qw(:DEFAULT :flock);

sysopen(DATA, $datafile, O_WRONLY|O_CREAT, 0666) or die "Error in subroutine writedata: Can't open $datafile: $!";
flock(DATA, LOCK_EX) or die "Error in subroutine writedata: Can't write-lock $datafile: $!";
truncate(DATA, 0) or die "Error in subroutine writedata: Can't truncate $datafile: $!";

print DATA @{$r_rows};

close(DATA);
}

#########################################################################
# #
# subroutine appenddata #
# #
#########################################################################

sub appenddata {

my ($datafile,$row) = @_;

use strict;

use 5.004;
use Fcntl qw(:DEFAULT :flock);

sysopen(DATA, $datafile, O_WRONLY|O_APPEND|O_CREAT, 0666) or die "Error in subroutine appenddata: Can't open $datafile: $!";;
flock(DATA, LOCK_EX) or die "Error in subroutine appenddata: Can't write-lock $datafile: $!";

print DATA "$row\n";

close(DATA);
}



davorg
Thaumaturge / Moderator

May 1, 2003, 1:30 AM

Post #2 of 3 (174 views)
Re: [mmcw] Sub routines using file locking [In reply to] Can't Post

Seems ok to me. Just a couple of questions...

1/ What is the "create_file" subroutine for? Surely the "writedata" and "appenddata" subroutines will create the file if it doesn't already exist.

2/ It's a bit pointless having the "use 5.004" and "use Fcntl qw(:DEFAULT :flock)" lines in every subroutine. They should only really appear once in the file.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


mmcw
User

May 1, 2003, 2:45 AM

Post #3 of 3 (173 views)
Re: [davorg] Sub routines using file locking [In reply to] Can't Post


In Reply To
1/ What is the "create_file" subroutine for? Surely the "writedata" and "appenddata" subroutines will create the file if it doesn't already exist.



Thats correct - I am removing it from the script data!




In Reply To


2/ It's a bit pointless having the "use 5.004" and "use Fcntl qw(:DEFAULT :flock)" lines in every subroutine. They should only really appear once in the file.



Thank you - I will write it to the main script!

Thank you for taking a look!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives