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


In reply to Any way to simulate a Windows path handling for File::Spec without Windows? by nysus

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.