in reply to Re^2: Perl appending \ or / based on windows or linux
in thread Perl appending \ or / based on windows or linux

You should be able to use a combination of File::Spec and a regex to dynamically determine what the seperation character is - provided that the regex contains all possible seperation characters.

use strict; use warnings; use File::Spec::Functions qw(:ALL); use feature 'say'; my $testpath = catdir('foo','bar'); #say $testpath; my $sepchar; ($sepchar) = ($testpath =~ m/(\\|\/)/); say $sepchar;

Not sure that this is any better than using $^O to determine the seperation character, but it one possible method using a core module. In either case, you still need to know the valid seperation characters for OSes that you intend to support. I guess the only advantage of the code above over using $^O is that you don't need to know the values of $^O for each OS.

Replies are listed 'Best First'.
Re^4: Perl appending \ or / based on windows or linux
by kennethk (Abbot) on Oct 07, 2014 at 20:39 UTC
    Rather than matching the separation character, match the directory names:
    use strict; use warnings; use File::Spec::Functions qw(:ALL); use feature 'say'; my $testpath = catdir('foo','bar'); #say $testpath; my ($sepchar) = $testpath =~ m/^foo(.+)bar$/ or die 'Separation character identification failed'; say $sepchar;

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      Yes, you and dasgar are probably right, if one needs to get the directory separator out of File::Spec.

      The only caveat I have heard of is that on some OSes, paths aren't that easy... here's a sample VMS filename I found: DKA0:[MYDIR.SUBDIR1.SUBDIR2]MYFILE.TXT;1 - interesting!