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