Pontifex
08-12-2007, 04:51 AM
I'm compiling some functions for a basic toolkit. Functions I use a lot in many different packages. I was doing some reading here:
http://perldoc.perl.org/perlopentut.html
Where I came across this:
"When is a file not a file? Well, you could say when it exists but isn't a plain file. We'll check whether it's a symbolic link first, just in case.
if (-l $file || ! -f _) {
print "$file is not a plain file\n";
}
What other kinds of files are there than, well, files? Directories, symbolic links, named pipes, Unix-domain sockets, and block and character devices. Those are all files, too--just not plain files. This isn't the same issue as being a text file. Not all text files are plain files. Not all plain files are text files. That's why there are separate -f and -T file tests."
My code is pretty simple, for reading files:
sub read_file ($) {
my ($file_name) = shift;
my $file_handle;
open ($file_handle, "<", $file_name) or return undef;
my @contents = <$file_handle>;
close ($file_handle);
return wantarray ? @contents : join (' ', @contents);
}#end of read_file ($)
But this has got me wondering if I should put a line like this before the open:
#check that file is a text file fit for reading
return undef unless (-f $file_name and -T $file_name);
I'm fairly certain that open would catch reading the non-text files detailed in the passage above and just throw an error. But I'm unsure of behavior of it reading a symlink or pipe. I don't want unexpected return values.
I suppose, I'm just asking for some advice on how to make my code there, more bullet proof and more style-alistically pleasing.
--Pontifex
http://perldoc.perl.org/perlopentut.html
Where I came across this:
"When is a file not a file? Well, you could say when it exists but isn't a plain file. We'll check whether it's a symbolic link first, just in case.
if (-l $file || ! -f _) {
print "$file is not a plain file\n";
}
What other kinds of files are there than, well, files? Directories, symbolic links, named pipes, Unix-domain sockets, and block and character devices. Those are all files, too--just not plain files. This isn't the same issue as being a text file. Not all text files are plain files. Not all plain files are text files. That's why there are separate -f and -T file tests."
My code is pretty simple, for reading files:
sub read_file ($) {
my ($file_name) = shift;
my $file_handle;
open ($file_handle, "<", $file_name) or return undef;
my @contents = <$file_handle>;
close ($file_handle);
return wantarray ? @contents : join (' ', @contents);
}#end of read_file ($)
But this has got me wondering if I should put a line like this before the open:
#check that file is a text file fit for reading
return undef unless (-f $file_name and -T $file_name);
I'm fairly certain that open would catch reading the non-text files detailed in the passage above and just throw an error. But I'm unsure of behavior of it reading a symlink or pipe. I don't want unexpected return values.
I suppose, I'm just asking for some advice on how to make my code there, more bullet proof and more style-alistically pleasing.
--Pontifex