Eric wrote a function to calculate depth of path in a filesystem.
Is everything OK here?
First of all, the code doesn’t compile.
The message is: Trailing \ in regex m/\/
. Why?
'\\'
is a string containing one character.
Even if you fix the Trailing \ in regex
, there is still a problem :(
There is an issue with split
in Windows part of the code.
It gets the string '\\'
as first parameter,
and the string is being interpolated into regex.
So it becomes:
which, of course, is incorrect.
It is a good practice to always pass an explicit regex as a first parameter for split
:
split m{\\}, $path
.
Besides, the function returns incorrect result for the root directory in Unix:
path_depth('/')
gives -1 instead of 0.