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):

use strict; use warnings; use Cwd; my $marker = '.marker'; cwd =~ m#^(.*)(?:/|$)(??{ print "at level '$1' \n"; ! -f "$1/$marker" +})# or die "'$marker' not found"; print "found '$marker' in '$1' \n";
Output (with no  .marker file present anywhere):
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'
If you have Perl version 5.10+, there's a variation that avoids this problem:
use 5.010; # needs (?(?{ CODE })yes-pattern) regex extension use strict; use warnings; use Cwd; my $marker = '.marker'; cwd =~ m{ \A (.*) (?: / | \z) # (??{ print "at level '$1' \n"; ! -f "$1/$marker"}) (?(?{ print "at level '$1' \n"; ! -f "$1/$marker" }) (*FAIL)) }xms or die "'$marker' not found"; print "found '$marker' in '$1' \n";
Output (still no  .marker file):
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.
Both versions of code behave the same if a  .marker is present at some level, although the first version will fail if the  .marker file is present above the level of, say, the  1nickt directory.


Give a man a fish:  <%-{-{-{-<


In reply to Re^3: Recursively walk up a directory tree by AnomalousMonk
in thread Recursively walk up a directory tree by 1nickt

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.