nysus has asked for the wisdom of the Perl Monks concerning the following question:
Got the following sub for creating what I call "short paths," which are relative file paths that are relative to a common directory shared by a group of arbitrary files. It's broken in Windows. I'd rather avoid having to use Windows to test it. Is there a way I can tell File::Spec to behave as if it's on a Windows machine?
use strict; use warnings; use File::Spec; my @files = qw ( C:\STRAWB~1\cpan\build\File-Collector-0.024-0\t\test_data\many_files\ +dir1\file4 C:\STRAWB~1\cpan\build\File-Collector-0.024-0\t\test_data\many_files\ +dir1\file5 C:\STRAWB~1\cpan\build\File-Collector-0.024-0\t\test_data\many_files\ +dir1\file6 C:\STRAWB~1\cpan\build\File-Collector-0.024-0\t\test_data\many_files\ +dir2\file7 C:\STRAWB~1\cpan\build\File-Collector-0.024-0\t\test_data\many_files\ +dir2\file8 C:\STRAWB~1\cpan\build\File-Collector-0.024-0\t\test_data\many_files\ +dir2\file9 C:\STRAWB~1\cpan\build\File-Collector-0.024-0\t\test_data\many_files\ +file1 C:\STRAWB~1\cpan\build\File-Collector-0.024-0\t\test_data\many_files\ +file2 C:\STRAWB~1\cpan\build\File-Collector-0.024-0\t\test_data\many_files\ +file3 ); &do(@files); sub do { my @files = @_; my $os = $^O; my $file = pop @files; my ($vol, $dirs) = File::Spec->splitpath( $file ); my @comps = File::Spec->splitdir( $dirs ); my ($new_string, $longest_string) = ''; foreach my $cfile (@files) { my ($cvol, $cdirs) = File::Spec->splitpath( $cfile ); my @ccomps = File::Spec->splitdir($cfile); my $lc = 0; foreach my $comp (@ccomps) { if (defined $comps[$lc] && $ccomps[$lc] eq $comps[$lc]) { $new_string .= File::Spec->catfile($ccomps[$lc++], ''); next; } $longest_string = $new_string; ($cvol, $cdirs) = File::Spec->splitpath( $new_string ); @comps = File::Spec->splitdir($cdirs); $new_string = ''; last; } } # $s->{_common_dir} = $longest_string || (fileparse($file))[1]; if (@files) { foreach my $file ( @files, $file ) { my $tfile = $file; $tfile =~ s/$longest_string//; # $s->{_files}{all}{$file}{short_path} = $tfile; } } else { # $s->{_files}{all}{$file}{short_path} = $file; } }
$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks
|
|---|