Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: File::Spec::Win32 returning different result than File::Spec when doing catfile with empty string

by nysus (Parson)
on Sep 08, 2019 at 16:18 UTC ( [id://11105838]=note: print w/replies, xml ) Need Help??


in reply to Re: File::Spec::Win32 returning different result than File::Spec when doing catfile with empty string
in thread File::Spec::Win32 returning different result than File::Spec when doing catfile with empty string

The algo just finds the greatest common relative directory to a group of files. Original code here. Current code is here.

$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

  • Comment on Re^2: File::Spec::Win32 returning different result than File::Spec when doing catfile with empty string

Replies are listed 'Best First'.
Re^3: File::Spec::Win32 returning different result than File::Spec when doing catfile with empty string
by jcb (Parson) on Sep 08, 2019 at 23:13 UTC

    Here is a quick solution that took me less time to write again than it would have taken to find where I wrote this before:

    #!/usr/bin/perl use strict; use warnings; use File::Spec; # Note that test data is Unix-format! warn "Test data is in Unix format; may not work on $^O" unless File::Spec->isa('File::Spec::Unix'); my @Files = map {chomp; $_} (<DATA>); # find common prefix directories for a list of files; assuming that al +l # files are on the same volume, if the system has volumes sub common_prefix (@) { my @dirs = map {[File::Spec->splitdir((File::Spec->splitpath($_))[1] +)]} @_; my @prefix = @{shift @dirs}; foreach my $dir (@dirs) { for (my $i = 0; $i < @$dir && $i < @prefix; $i++) { splice @prefix, $i unless $dir->[$i] eq $prefix[$i] } } return @prefix } print "Sample files:\n"; print ' ',$_,"\n" for @Files; print "Common prefix:\n"; print ' ',File::Spec->catdir(common_prefix @Files),"\n"; __DATA__ /foo/bar/bax /foo/bar/baz /foo/baz/quux

    Sample output:

    Sample files: /foo/bar/bax /foo/bar/baz /foo/baz/quux Common prefix: /foo
      warn "Test data is in Unix format; may not work on $^O" unless File::Spec->isa('File::Spec::Unix');

      Fails on Win32, because File::Spec::Win32 inherits from File::Spec::Unix (see line 11 of File::Spec::Win32 v3.75). Another design issue in File::Spec (in addition to case_tolerant()). File::Spec::Unix is also used as base class for other, unrelated operating systems, instead of using an explicit base class.

      Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\alex>perl -MFile::Spec -E "say File::Spec->isa('File::Spec::W +in32')" 1 C:\Users\alex>perl -MFile::Spec -E "say File::Spec->isa('File::Spec::U +nix')" 1 C:\Users\alex>

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

        Fun... does $File::Spec::ISA[0] eq 'File::Spec::Unix' work reliably, then?

      Thanks so much!

      $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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-23 06:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found