I have a Perl script that is supposed to monitor a pre-defined list of UNC paths and make sure there are no files waiting to be processed in those directories. The first thing the script does is read in a list of UNC's (in the sample below - from DATA). It then puts these UNC's into a Hash of Arrays (no problem so far - everything work nicely). Next, I want to loop through each of those array's and scan the directory. When I pull the UNC's back out of the array, I run into problems, mainly where the "\\" at the start is escaped down to "\" (I think this is because of the single quote that I am inserting to account for the spaces in the file names). I don't have any control over the names in the UNCs - but if I need to tweak the data a bit to account for spaces and quotes in the paths, I can do that.

My code so far (streamlined to display problem):

use File::Spec; use Data::Dumper; use strict; use warnings; my @dir_list; my $rec; my %HoA; foreach my $dir (<DATA>) { chomp $dir; my $first=substr($dir,0,1); if ($first =~ /#/) { next; } elsif ( $first =~ /=/ ) { if ($dir=~/(^\={2})(\w+)(\={2})/) { $rec=$2; } @dir_list=(); #reset the list of directories to empty } else { $dir=File::Spec->catdir("\'$dir\'"); push @dir_list, $dir; #add directory name to array $HoA{$rec}=[ @dir_list ]; #add array of directories to + HoA }#close if }#close foreach ###################################################################### +####################################### print Data::Dumper->Dump([\%HoA], ['*HoA']); for my $server ( keys %HoA ) { chomp $server; for my $dir ( <@{ $HoA{$server} }> ) { chomp $dir; print "$server: $dir\n"; } } __DATA__ ########## Server 1 UNC's ######################## ==SERVER1== \\server1\share1\tld0\sub-dir1\sub-dir2 \\server1\share1\tld0\sub-dir2\sub-dir2 \\server1\share2\tld1\dir1\subdir2\subdir3 \\server1\share2\tld1\dir1\subdir2\subdir4 ########## Other Server UNC's ######################## ==REMOTESERVER1== \\remoteserver1\share1\tld1\dir1\subdir2\subdir4 ==REMOTESERVER2== \\remoteserver2\share1\tld1\dir1\sub dir 3\sub dir 5\report's \\remoteserver2\share1\tld1\dir2\sub dir 4\sub dir 6\reports ###### No Entries Below This Line ######
Here is my output:

%HoA = ( 'REMOTESERVER1' => [ '\'\\remoteserver1\\share1\\tld1\\dir1\\ +subdir2\\subdir4\'' ], 'SERVER1' => [ '\'\\server1\\share1\\tld0\\sub-dir1\\sub-dir2 +\'', '\'\\server1\\share1\\tld0\\sub-dir2\\sub-dir2 +\'', '\'\\server1\\share2\\tld1\\dir1\\subdir2\\sub +dir3\'', '\'\\server1\\share2\\tld1\\dir1\\subdir2\\sub +dir4\'' ], 'REMOTESERVER2' => [ '\'\\remoteserver2\\share1\\tld1\\dir1\\ +sub dir 3\\sub dir 5\\report\'s\'', '\'\\remoteserver2\\share1\\tld1\\dir2\\ +sub dir 4\\sub dir 6\\reports\'' ] ); REMOTESERVER1: '\remoteserver1\share1\tld1\dir1\subdir2\subdir4' SERVER1: \server1\share1\tld0\sub-dir1\sub-dir2 SERVER1: \server1\share1\tld0\sub-dir2\sub-dir2 SERVER1: \server1\share2\tld1\dir1\subdir2\subdir3 SERVER1: \server1\share2\tld1\dir1\subdir2\subdir4 REMOTESERVER2: '\remoteserver2\share1\tld1\dir1\sub dir 3\sub dir 5\re +port's' '\remoteserver2\share1\tld1\dir2\sub dir 4\sub dir 6\reports'

What I expect to see in the output would be something along the lines of:

REMOTESERVER1: \\remoteserver1\share1\tld1\dir1\subdir2\subdir4 SERVER1: \\server1\share1\tld0\sub-dir1\sub-dir2 SERVER1: \\server1\share1\tld0\sub-dir2\sub-dir2 SERVER1: \\server1\share2\tld1\dir1\subdir2\subdir3 SERVER1: \\server1\share2\tld1\dir1\subdir2\subdir4 REMOTESERVER2: \\remoteserver2\share1\tld1\dir1\sub dir 3\sub dir 5\re +port's REMOTESERVER2: \\remoteserver2\share1\tld1\dir2\sub dir 4\sub dir 6\re +ports

Any thoughts on working through this issue would be appreciated. I have tried wrapping single and double quotes around the UNC's, flipping the backslashes to forward slashes and nothing really seems to work (a few things make it worse such as removing the directory seperators completely).

update: figured out the problem as thundergnat points out below.


In reply to Windows Paths by nimdokk

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.