E
Eric Martin
I am processing multiple files and am trying to create routines that
will efficiently read/write files. I am using variable names for the
FILEHANDLE to prevent any conflicts during processing (Is this a good
practice or is there a *better* way?).
use strict; use warnings;
no strict 'refs';
use Fcntl qw
DEFAULT :flock);
sub file_open {
my ($fh, $file, $mode) = @_;
open($fh, "$mode $file") || die("Cannot open $file: $!");
flock($fh, LOCK_EX);
return \*$fh;
}
sub file_close {
my $fh = shift;
close($fh);
}
my $file = "data.txt";
my $fh = file_open("TEST_OUT", $file, '+<');
my $out = file_open("TEST_IN", "data_out.txt", '+>');
while (<$fh>) {
# process data
# ...
print $out $_;
}
file_close($out);
file_close($fh);
__END__
I tried:
sub file_open {
my ($fh, $file, $mode) = @_;
my $filehandle = do { local *$fh }; # <-- ADDED
open($filehandle, "$mode $file") || die("Cannot open $file: $!");
flock($filehandle, LOCK_EX);
return \*$filehandle;
}
which I saw in an example. I've seen it used for file "slurping", but I
am not sure if it is something I should use. My understanding is that
it is used to make the FILEHANDLE local, but in the context that I am
using, it doesn't seem necessary.
I know there are modules out there to use, but I am not using them
because I need the code to work with the standard 5.004 Perl install.
I'm looking for any code/design comments. Anything that I'm not doing
or that I should be doing?
Thanks,
Eric
will efficiently read/write files. I am using variable names for the
FILEHANDLE to prevent any conflicts during processing (Is this a good
practice or is there a *better* way?).
use strict; use warnings;
no strict 'refs';
use Fcntl qw
sub file_open {
my ($fh, $file, $mode) = @_;
open($fh, "$mode $file") || die("Cannot open $file: $!");
flock($fh, LOCK_EX);
return \*$fh;
}
sub file_close {
my $fh = shift;
close($fh);
}
my $file = "data.txt";
my $fh = file_open("TEST_OUT", $file, '+<');
my $out = file_open("TEST_IN", "data_out.txt", '+>');
while (<$fh>) {
# process data
# ...
print $out $_;
}
file_close($out);
file_close($fh);
__END__
I tried:
sub file_open {
my ($fh, $file, $mode) = @_;
my $filehandle = do { local *$fh }; # <-- ADDED
open($filehandle, "$mode $file") || die("Cannot open $file: $!");
flock($filehandle, LOCK_EX);
return \*$filehandle;
}
which I saw in an example. I've seen it used for file "slurping", but I
am not sure if it is something I should use. My understanding is that
it is used to make the FILEHANDLE local, but in the context that I am
using, it doesn't seem necessary.
I know there are modules out there to use, but I am not using them
because I need the code to work with the standard 5.004 Perl install.
I'm looking for any code/design comments. Anything that I'm not doing
or that I should be doing?
Thanks,
Eric