Home > Mobile >  `-d _` works fine, but `-l _` does not work. Why it returns `The stat preceding -l _ wasn't an
`-d _` works fine, but `-l _` does not work. Why it returns `The stat preceding -l _ wasn't an

Time:09-01

Look at two examples:

$ perl -e ' print -e "registrar" && -d _ ? "YES" : "NO" '
YES

$ perl -e ' print -e "registrar" && -l _ ? "YES" : "NO" '
The stat preceding -l _ wasn't an lstat at -e line 1.

-d and -l are both file test operators. Why second does not work? The stat preceding -l _ is same as for -d _.

CodePudding user response:

Most of the file tests use stat, which follows symlinks; -l uses lstat because it makes no sense to follow one if you want to test if it itself is a symbolic link. Hence the error.

Also, as of perl 5.10, tests can be directly chained without needing to be &&ed together. Doing this with -e and -l works since perl is smart enough to pick the appropriate stat function:

$ mkdir registrar
$ perl -E ' say -e -d "registrar" ? "YES" : "NO" '
YES
$ perl -E ' say -e -l "registrar" ? "YES" : "NO" '
NO
  •  Tags:  
  • perl
  • Related