Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Single Quotes - how to avoid any escape processing?

by SteveDC (Initiate)
on Aug 25, 2019 at 18:10 UTC ( [id://11104993]=note: print w/replies, xml ) Need Help??


in reply to Single Quotes - how to avoid any escape processing?

Although this is an old thread I think it aligns well with a current issue I have whilst processing Windows paths. I have spent the last hour scouring THE WEB and can't for the life of me find a clean solution to my issue. Basically what I want to do is split a path into a list of each sub directory, so in its simplest form I tried the following...
$PathOnly = "c:\Dir1\Dir2\Dir3"; @SubDirs = split('\', $PathOnly);
This doesn't work since the \' gets escaped and I end up effectively with a missing ' So, tried to escape the \ with the following...
@SubDirs = split('\\', $PathOnly);
But that also has an error "trailing \ in regex" As does the following...
@SubDirs = split("\\", $PathOnly);
I even tried the following...
my $SplitAt = "\\"; my @SubDirs = split($SplitAt, $PathOnly);
Whilst this doesn't have syntax errors, and $SplitAt seems to get theexpected "\" it fails on the execution of the split with the same "trailing \ in regex", just at execution time this time. Now... just for kicks and giggles I tried the following...
my $SplitAt = "\\\\"; my @SubDirs = split($SplitAt, $PathOnly);
BOOM !!! I get the correct result (I actually thought of this whilst typing the comment but figured the info might help someone at some point in time) So, then I tried the following...
my @SubDirs = split("\\\\", $PathOnly);
This also worked as I wanted. So, my original question has changed from "How do I split a Windows path on \ ?" to "Why does "\\\\" work in the split statement? I have been working with Perl on and off for about 20 years but I am by no means an expert, but this conundrum completely confuses me. BR, Steve

Replies are listed 'Best First'.
Re^2: Single Quotes - how to avoid any escape processing?
by Corion (Patriarch) on Aug 25, 2019 at 18:25 UTC

    split does not take a string as its first argument, it takes a regular expression.

    So the correct usage is

    @SubDirs = split(/\\/, $PathOnly);

    But maybe you want to use File::Spec instead, or Path::Tiny?

    my @SubDirs = File::Spec->splitdir( $PathOnly );
Re^2: Single Quotes - how to avoid any escape processing?
by AnomalousMonk (Archbishop) on Aug 25, 2019 at 18:38 UTC

    Either of the literals  "\\\\" or  '\\\\' interpolate in a  m// or  qr// operator to a pair of backslash characters  \\ (escaped-backslash) which match a single backslash character.

    c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "print '\\'; print '\\\\'; print qq{\\}; print qq{\\\\}; ;; print qr{\\}; my $bb = '\\\\'; print qr{$bb}; ;; my $PathOnly = 'c:\Dir1\Dir2\Dir3'; ;; my @SubDirs = split(m/\\/, $PathOnly); dd \@SubDirs; ;; @SubDirs = split($bb, $PathOnly); dd \@SubDirs; " \ \\ \ \\ (?-xism:\\) (?-xism:\\) ["c:", "Dir1", "Dir2", "Dir3"] ["c:", "Dir1", "Dir2", "Dir3"]


    Give a man a fish:  <%-{-{-{-<

Re^2: Single Quotes - how to avoid any escape processing?
by soonix (Canon) on Aug 25, 2019 at 18:37 UTC

    Corion already commented on the problem you noted, but there's another: your $PathOnly is in double quotes, so the backslashes there are interpreted as escape sequences.

    Also, should you ever be in need of a string consisting of a single backslash, you could use "\N{REVERSE SOLIDUS}" (prior to v5.16, you have to use charnames explicitly for this to work). That is complicated, but whoever reads that, will have no doubt that you indeed want a single backslash that is not part of an escape secuence.
      ... your $PathOnly is in double quotes, so the backslashes there are i +nterpreted as escape sequences.

      SteveDC:   Ah, yes!

      c:\@Work\Perl\monks>perl -wMstrict -le "my $PathOnly = qq{c:\Dir1\Dir2\taint_so}; print qq{>$PathOnly<}; " Unrecognized escape \D passed through at -e line 1. Unrecognized escape \D passed through at -e line 1. >c:Dir1Dir2 aint_so<
      Don't know how you missed the warnings. You do have warnings enabled, right? Right?


      Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11104993]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-04-23 11:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found