1nickt has asked for the
wisdom of the Perl Monks concerning the following question:

Greetings all,

I need to walk up the directory tree from where I am until I find a directory containing a certain marker file. (This is to locate the base directory of an installation, which is then used to add the project's /lib to @INC.)

The cwd() function gives the full path of the... well, CWD. The match looks for a file at the end (rightmost part) of the path. If the file is not found, the regex engine effectively backtracks up a level in the directory.

The ! -f "$1/.marker" code in the (??{ CODE }) construct evaluates to '' (empty string) if the file exists at a given level, and this is interpolated into the regex pattern and always matches, hence the file is "found" at that level.

If the file is not found, the negated file test evaluates to '1' and this will probably not be found at the beginning of a directory name. However, this can fail (debug print added to show progress):

c:\@Work\Perl\monks\1nickt\two\one\zero>perl find_marker_1.pl
at level 'c:/@Work/Perl/monks/1nickt/two/one/zero'
at level 'c:/@Work/Perl/monks/1nickt/two/one'
at level 'c:/@Work/Perl/monks/1nickt/two'
at level 'c:/@Work/Perl/monks/1nickt'
at level 'c:/@Work/Perl/monks'
found '.marker' in 'c:/@Work/Perl/monks'

c:\@Work\Perl\monks\1nickt\two\one\zero>perl find_marker_2.pl
at level 'c:/@Work/Perl/monks/1nickt/two/one/zero'
at level 'c:/@Work/Perl/monks/1nickt/two/one'
at level 'c:/@Work/Perl/monks/1nickt/two'
at level 'c:/@Work/Perl/monks/1nickt'
at level 'c:/@Work/Perl/monks'
at level 'c:/@Work/Perl'
at level 'c:/@Work'
at level 'c:'
'.marker' not found at find_marker_2.pl line 62.