unless (-e $file) {
open FILE, "> $file";
# ...
}
because that creates a race condition. Instead, use the sysopen() function.
use POSIX qw( O_EXCL O_CREAT );
$perm = 0644; # or whatever
sysopen FILE, $file, O_EXCL | O_CREAT, $perm or die "can't create $fil
+e: $!";
japhy --
Perl and Regex Hacker | [reply] [d/l] [select] |
I've been asked to specify what I mean. The problem is that the file might spring into existence between the time you test for existence, and when you create it. That's because there's time in between the -e $file and the open().
To get around that, you can use sysopen(), which is atomic, which means that the check to see if the file is there, and the opening of the file, are done instantaneously.
Re: Re: is there a file/directory?
With mkdir(), you don't have that problem:
if (!mkdir($dir, $perm) and $! =~ /^File exists/) {
warn "($dir already exists)";
}
elsif ($! =~ /^Permission denied/) {
warn "(can't create $dir)";
}
# ...
japhy --
Perl and Regex Hacker | [reply] [d/l] |
See the file test operators, specifically -d.
Update: though I don't know why I'd point out -d when you asked about files also, for which -e is handy...
--
I'd like to be able to assign to an luser | [reply] [d/l] |
my $file = "/etc/passwd";
my $directory = "/etc";
if (-f $file) { print "file !!" }
if (-d $directory) { print "directory !!" }
-e can be used to verify it actually exists, altho both -d and -f will fail if the file/directory not exists...
Greetz
Beatnik
... Quidquid perl dictum sit, altum viditur. | [reply] [d/l] [select] |
If you don't need to know exactly why your program failed,
try this
open(INFILE, "infile") && !(-e "outfile") &&
open (OUTFILE, ">outfile") || die ("Cannot open files\n");
| [reply] [d/l] |
Use the -efile test operator:
if(-e $file){
print"File exists\n";
}
## And to test if its a directory...
if(-d $file){
print"Its also a directory!\n";
}
TStanley
--------
There's an infinite number of monkeys outside who want to talk to us
about this script for Hamlet they've worked out -- Douglas Adams/Hitchhiker's Guide to the Galaxy
| [reply] [d/l] [select] |
You would be looking for the -e file test. More information can be found on the perlfunc node
Ooohhh, Rob no beer function well without! | [reply] |
for a file u can use
print -f 'filename';
for directory check u can use
print -d 'directory name'; | [reply] |