http://www.perlmonks.org?node_id=309652

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

Howdy monks,

I came across this code in an open-source module today, and I've been trying to understand what it does. I'm refactoring the model so it works under -w and one line (marked in code with # GIVES WARNING #) gives:

\1 better written as $1 at TempFileNames.pm line 209.

I wanted to make sure I understood the whole sub (not just the reg-ex) before making any changes. The author is unavailable for a few days, so I thought I'd get some guidance on what's going on here faster. All comments are direct from the code, and aren't mine.

sub normalizedPath { my ($path, $sep, $doSlashes, $beURLaware) = @_; # URLawareness means that m{[a-z]+//:} will be skipped # if ($beURLaware) then exclude the double slashed url-qualifier if ($doSlashes) { if ($beURLaware) { my ($type, $protocol, $urlPath) = ($path =~ m{(([a-z]+:)?//)?(.*)}o); $urlPath =~ s{/+}{/}go; $path = $type.$urlPath; # $path = ($protocol ne ''? $protocol: 'http:')."//$urlPath"; # if ($beURLaware) { # my ($type, $urlPath) = ($path =~ m{([a-z]+://)?(.*)}o); # $urlPath =~ s{/+}{/}go, $path = $type.$urlPath; } else { $path=~s{/+}{/}go; } #mult slashes are like single slashes } # . elimination before .. dt .. must ignore any . # eliminate . components: here seems to be some bug <b> $path=~s{(^|/)\./}{\1}og; #g option is safe here (dt limited scope) # Line below gives warning while ($path=~s{(^|/)[^/]*/\.\./}{\1}o) {} #<A><b> suceeding '..' prev +ent g option # Line above gives Warning return $path; }

Any thoughts on rewriting this? I'm not quite happy to just change the \1 to $1, without understanding what's going on.

-Tats