Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Help interpreting this code

by Itatsumaki (Friar)
on Nov 24, 2003 at 19:48 UTC ( #309652=perlquestion: print w/replies, xml ) Need Help??

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

Replies are listed 'Best First'.
Re: Help interpreting this code
by Anonymous Monk on Nov 24, 2003 at 20:02 UTC

    When you get warnings you do not understand, try using the diagnostics module:

    \1 better written as $1 at - line 3 (#1) (W syntax) Outside of patterns, backreferences live on as variables. The use of backslashes is grandfathered on the right-hand side of a substitution, but stylistically it's better to use the variable form because other Perl programmers will expect it, and it works better i +f there are more than 9 backreferences.
Re: Help interpreting this code
by Abigail-II (Bishop) on Nov 24, 2003 at 19:59 UTC
    Replacing \1 with $1 is the right thing to do. In both cases.

    Abigail

Re: Help interpreting this code
by duff (Parson) on Nov 24, 2003 at 20:18 UTC

    Something that jumps right out at me are those /o modifiers. They are quite useless. I'd get rid of them

Re: Help interpreting this code
by dragonchild (Archbishop) on Nov 24, 2003 at 20:49 UTC
    What module is this? It looks like it's parsing a URI. If it's not URI, I'm very curious why it's not using URI ...

    ------
    We are the carpenters and bricklayers of the Information Age.

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    ... strings and arrays will suffice. As they are easily available as native data types in any sane language, ... - blokhead, speaking on evolutionary algorithms

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      This some very cool code I found available to answer my question about dynamic chromosome drawing. It's available at this site. I'm adding configuration files for rat and mouse genomes, and thought cleaning out the warnings would make debugging problems a little easier.

Re: Help interpreting this code
by Itatsumaki (Friar) on Nov 24, 2003 at 23:30 UTC

    I made the changes, and all tests well: thanks for the help!

    -Tats

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://309652]
Approved by mpeppler
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2020-05-26 18:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (150 votes). Check out past polls.

    Notices?