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

In reply to Help interpreting this code by Itatsumaki

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.