This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
file spec tweaks for VMS
[perl5.git] / lib / lib_pm.PL
index 66b4944..f6bb665 100644 (file)
@@ -30,7 +30,7 @@ if ($ENV{PERL_BUILD_EXPAND_CONFIG_VARS}) {
     $Config_archname = q($Config{archname});
     $Config_version  = q($Config{version});
     $Config_inc_version_list =
-             q(reverse split / /, qw($Config{inc_version_list}));
+             q(reverse split / /, $Config{inc_version_list});
 }
  
 open OUT,">$file" or die "Can't create $file: $!";
@@ -48,6 +48,8 @@ package lib;
 
 $useConfig
 
+use strict;
+
 my \$archname         = $Config_archname;
 my \$version          = $Config_version;
 my \@inc_version_list = $Config_inc_version_list;
@@ -57,31 +59,46 @@ print OUT <<'!NO!SUBS!';
 
 our @ORIG_INC = @INC;  # take a handy copy of 'original' value
 our $VERSION = '0.5564';
+my $Is_MacOS = $^O eq 'MacOS';
+my $Mac_FS;
+if ($Is_MacOS) {
+       require File::Spec;
+       $Mac_FS = eval { require Mac::FileSpec::Unixish };
+}
 
 sub import {
     shift;
 
     my %names;
     foreach (reverse @_) {
-       if ($_ eq '') {
+       my $path = $_;          # we'll be modifying it, so break the alias
+       if ($path eq '') {
            require Carp;
            Carp::carp("Empty compile time value given to use lib");
        }
-       if (-e && ! -d _) {
+
+       $path = _nativize($path);
+
+       if (-e $path && ! -d _) {
            require Carp;
            Carp::carp("Parameter to use lib must be directory, not file");
        }
-       unshift(@INC, $_);
-        # Add any previous version directories we found at configure time
-        foreach my $incver (@inc_version_list)
-        {
-            unshift(@INC, "$_/$incver") if -d "$_/$incver";
-        }
-       # Put a corresponding archlib directory infront of $_ if it
-       # looks like $_ has an archlib directory below it.
-       unshift(@INC, "$_/$archname")          if -d "$_/$archname/auto";
-       unshift(@INC, "$_/$version")           if -d "$_/$version";
-       unshift(@INC, "$_/$version/$archname") if -d "$_/$version/$archname";
+       unshift(@INC, $path);
+       # Add any previous version directories we found at configure time
+       foreach my $incver (@inc_version_list)
+       {
+           my $dir = $Is_MacOS
+               ? File::Spec->catdir( $path, $incver )
+               : "$path/$incver";
+           unshift(@INC, $dir) if -d $dir;
+       }
+       # Put a corresponding archlib directory in front of $path if it
+       # looks like $path has an archlib directory below it.
+       my($arch_auto_dir, $arch_dir, $version_dir, $version_arch_dir)
+           = _get_dirs($path);
+       unshift(@INC, $arch_dir)         if -d $arch_auto_dir;
+       unshift(@INC, $version_dir)      if -d $version_dir;
+       unshift(@INC, $version_arch_dir) if -d $version_arch_dir;
     }
 
     # remove trailing duplicates
@@ -95,10 +112,14 @@ sub unimport {
 
     my %names;
     foreach (@_) {
+       local $_ = _nativize($_);
+
+       my($arch_auto_dir, $arch_dir, $version_dir, $version_arch_dir)
+           = _get_dirs($_);
        ++$names{$_};
-       ++$names{"$_/$archname"}          if -d "$_/$archname/auto";
-       ++$names{"$_/$version"}           if -d "$_/$version";
-       ++$names{"$_/$version/$archname"} if -d "$_/$version/$archname";
+       ++$names{$arch_dir}         if -d $arch_auto_dir;
+       ++$names{$version_dir}      if -d $version_dir;
+       ++$names{$version_arch_dir} if -d $version_arch_dir;
     }
 
     # Remove ALL instances of each named directory.
@@ -106,6 +127,37 @@ sub unimport {
     return;
 }
 
+sub _get_dirs {
+    my($dir) = @_;
+    my($arch_auto_dir, $arch_dir, $version_dir, $version_arch_dir);
+
+    # we could use this for all platforms in the future, but leave it
+    # Mac-only for now, until there is more time for testing it.
+    if ($Is_MacOS) {
+       $arch_auto_dir    = File::Spec->catdir( $_, $archname, 'auto' );
+       $arch_dir         = File::Spec->catdir( $_, $archname, );
+       $version_dir      = File::Spec->catdir( $_, $version );
+       $version_arch_dir = File::Spec->catdir( $_, $version, $archname );
+    } else {
+       $arch_auto_dir    = "$_/$archname/auto";
+       $arch_dir         = "$_/$archname";
+       $version_dir      = "$_/$version";
+       $version_arch_dir = "$_/$version/$archname";
+    }
+    return($arch_auto_dir, $arch_dir, $version_dir, $version_arch_dir);
+}
+
+sub _nativize {
+    my($dir) = @_;
+
+    if ($Is_MacOS && $Mac_FS && ! -d $dir) {
+       $dir = Mac::FileSpec::Unixish::nativize($dir);
+       $dir .= ":" unless $dir =~ /:$/;
+    }
+
+    return $dir;
+}
+
 1;
 __END__
 
@@ -171,6 +223,22 @@ can say
 
     @INC = @lib::ORIG_INC;
 
+=head1 CAVEATS
+
+In order to keep lib.pm small and simple, it only works with Unix
+filepaths.  This doesn't mean it only works on Unix, but non-Unix
+users must first translate their file paths to Unix conventions.
+
+    # VMS users wanting to put [.stuff.moo] into 
+    # their @INC would write
+    use lib 'stuff/moo';
+
+=head1 NOTES
+
+In the future, this module will likely use File::Spec for determining
+paths, as it does now for Mac OS (where Unix-style or Mac-style paths
+work, and Unix-style paths are converted properly to Mac-style paths
+before being added to @INC).
 
 =head1 SEE ALSO