mikfire has asked for the wisdom of the Perl Monks concerning the following question:
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 )
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Normalized directory paths
by lhoward (Vicar) on May 16, 2000 at 22:13 UTC | |
|
Re: Normalized directory paths
by mikfire (Deacon) on May 16, 2000 at 21:37 UTC | |
|
Re: Normalized directory paths
by chromatic (Archbishop) on May 16, 2000 at 23:50 UTC | |
by mikfire (Deacon) on May 17, 2000 at 00:15 UTC | |
by znik (Initiate) on Feb 16, 2016 at 08:02 UTC | |
by turnstep (Parson) on May 17, 2000 at 00:51 UTC | |
|
RE: Normalized directory paths
by merlyn (Sage) on May 17, 2000 at 02:20 UTC | |
by turnstep (Parson) on May 17, 2000 at 02:23 UTC | |
by mikfire (Deacon) on May 17, 2000 at 16:24 UTC | |
by merlyn (Sage) on May 17, 2000 at 20:03 UTC | |
by mikfire (Deacon) on May 17, 2000 at 22:11 UTC | |
|
Re: Normalized directory paths
by ZZamboni (Curate) on May 16, 2000 at 21:53 UTC | |
|
Re: Normalized directory paths
by turnstep (Parson) on May 16, 2000 at 21:51 UTC | |
by mikfire (Deacon) on May 16, 2000 at 21:53 UTC |