Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

how do i check to see whether a file/directory is actually on the system before i try to write over it?

Replies are listed 'Best First'.
Re: is there a file/directory?
by japhy (Canon) on May 29, 2001 at 21:15 UTC
    I don't suggest you use
    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
      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
Re: is there a file/directory?
by Albannach (Monsignor) on May 29, 2001 at 21:12 UTC
    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

Re: is there a file/directory?
by Beatnik (Parson) on May 29, 2001 at 21:15 UTC
    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.
Re: is there a file/directory?
by Sexy Nun (Initiate) on May 29, 2001 at 22:43 UTC

    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");
Re: is there a file/directory?
by TStanley (Canon) on May 29, 2001 at 21:17 UTC
    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
Re: is there a file/directory?
by rob_au (Abbot) on May 29, 2001 at 21:16 UTC
    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!

Re: is there a file/directory?
by perlboy (Initiate) on May 29, 2001 at 22:39 UTC
    for a file u can use print -f 'filename'; for directory check u can use print -d 'directory name';