-1

I want to check if pass file exist and there is no fail file in certain path. Is there any operator for file not exist?

example path:
x.pass
y.fail

my $filepath = "path/to/file/";
if (-e "$filepath/*pass" && <*fail not exist>){
  #do something
}
walker
  • 157
  • 5
  • 3
    `if ( ! -e path ) ...` – LeGEC Mar 06 '23 at 06:09
  • 1
    `-e` is the operator for "File exists" and you are using it already. Your if-statement basically would check then if "File exists && File does not exist" - which is always false. – Steffen Ullrich Mar 06 '23 at 06:09
  • 2
    In case anyone wonders, the OP's question is more like "file1 exists, and file2 doesn't exist" – qrsngky Mar 06 '23 at 06:15
  • Nope, no such operator. Can use `not -e $file` (or `! -e $file` for tighter precedence). Watch for precedence when using complex conditions; that `&&` binds more tightly than `and`. For example, `if (-e $f1 and not -e $f2)` is fine. When in doubt use extra parenthesis. And test carefully. – zdim Mar 06 '23 at 07:10
  • (Regarding the mention of precedence in my previous comment-- In this particular case precedence rules don't matter, as `&& not` can't do anything other than what is needed -- `not` still negates what follows it and then that is `&&`-ed with the preceding condition. But it would make me very uncomfortable seeing the higher precedence `&&` tying conditions which themselves use the lower precedence `not` (or others). It's errors waiting to happen) – zdim Mar 06 '23 at 07:24
  • thanks for the suggestion. I will use ! e in this case – walker Mar 06 '23 at 07:48

1 Answers1

0

stat/-e gets information about a file. It returns false if there's an error, including if it doesn't exist. It returns error ENOENT if the file doesn't exist.

if ( !-e $path ) {
   die( "`$path` doesn't exist\n" ) if $!{ENOENT};  # Optional.
   die( "Can't stat `$path`: $!\n" );
}

or

if ( !stat( $path ) ) {
   die( "`$path` doesn't exist\n" ) if $!{ENOENT};  # Optional.
   die( "Can't stat `$path`: $!\n" );
}

Note that you ask how to check if a file exists. If you're asking about globs as your example suggests, you already know how to do that.

ikegami
  • 367,544
  • 15
  • 269
  • 518