I have toyed with this an awful lot today and I arrived at something like:
This was not satisfying - the m## and then an s### with exactly the same arguments was lacking. Remembering that s### returns true if something was replaced, I was able to reduce this to# NOTE: I have already prepended pwd if needed #remove . while( $path =~ m#/\./# ) { $path =~ s#/\.#/#; } # remove .. while( $path =~ m#/[^/]+?/\.\./# ) { $path =~ s#/[^/]+?/\.\.#/#; }
But this still does not please me. Using two loops when I feel I should only have to use one. Working a bit harder and unleashing the marvelous /e modifier, I was able to finally say#remove . while ( $path =~ s#/\./#/# ) {;} #remove .. while ( $path =~ s#/[^/]+?/\.\./#/# ) {;}
I am pleased but wonder what other ways the honored Monks have found to do this - my solution is likely to be a real CPU pig.while ( $path =~ s#(/[^/]+)?/\.(\.)?/defined($2) ? "/" : "$1/"/e ) {;}
For reasons I cannot elucidate, this solution is right out
To further the discussion, I will point out that using the /g modifier does not work - the regex engine does double quotish expansion only once and will not reapply the regex to the new string. A wise move, considering the evil deep recursions that could result.unless ( chdir $path ) { die "Couldn't there ($path) from here\n"; } return `pwd`;
Mik
Mik Firestone (perlus bigotous maximus )
In reply to Normalized directory paths by mikfire
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |