Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if file is NOT a directory in Perl

I'm aware you can test if a file is a directory using:

if(-d $filename)

but how can you test if it's not a directory?

like image 490
sj755 Avatar asked Oct 11 '11 02:10

sj755


People also ask

How do you check if it is a file or directory in Perl?

The quickest way to tell files from directories is to use Perl's built-in ​File Test Operators. Perl has operators you can use to test different aspects of a file. The -f operator is used to identify regular files rather than directories or other types of files.

How do you check if a file does not exists in Perl?

Perl has a set of useful file test operators that can be used to see whether a file exists or not. Among them is -e, which checks to see if a file exists.

How do I search for a file in a directory in Perl?

Traversing files and directories in Perl can also be done through File::Find module which comes with the Perl language. File::Find contains 2 modules: Find: find() function performs a depth-first search on the mentioned/defined @directories.

What is =~ in Perl?

The '=~' operator is a binary binding operator that indicates the following operation will search or modify the scalar on the left. The default (unspecified) operator is 'm' for match. The matching operator has a pair of characters that designate where the regular expression begins and ends.


2 Answers

Have you thought of trying the following?

if (! -d $filename) ...

The result of -d is, after all, something that can be treated as a boolean value, hence the logical not operator ! will work fine.

And, if you're after something more specific than "not a directory", see here. There are quite a few things that aren't directories that it sometimes doesn't make sense to treat as regular files.

Keep in mind that's assuming you've already validated that the filename exists. If you do a ! -d on a non-existent filename, it will return true.

It's a philosophical matter as to whether you consider a non-existent thing to be "not a directory" but, if you want to ensure it exists as well, you can use ! -d in conjunction with -e, something like:

if ((-e $filename) && (! -d $filename)) ...
like image 97
paxdiablo Avatar answered Sep 22 '22 17:09

paxdiablo


To test if something is a file, BUT not a directory, you need to simply combine the 2 tests (there's no single test):

if (-e $file && !-d $file) { # a file but not a directory }

Please note that:

  • With all due respect, paxdiablo's answer is wrong for the way the question is worded ( pure !-d does not work for what the user asked as it checks if a random thing/string is not a directory, NOT whether something is a file which is not a directory). E.g. !-d NO_SUCH_FILE returns true.

  • Greg's answer may be correct depending entirely on whether the original user meant to include non-plain files (e.g. symbolic links, named pipes, etc..) in their definition of "file which is not a directory". If they meant to include all those special "files", Greg's answer is also wrong as "-f" excludes them (as Greg wisely noted in his answer in the first place).

like image 28
DVK Avatar answered Sep 21 '22 17:09

DVK