develooper Front page | perl.perl5.porters | Postings from December 2001

[PATCH lib/lib_pm.PL lib/lib.t] portability snag

Thread Next
From:
Michael G Schwern
Date:
December 19, 2001 04:42
Subject:
[PATCH lib/lib_pm.PL lib/lib.t] portability snag
Message ID:
20011219124254.GF8630@blackrider
I've adapted Brent's lib.t test to be portable (see below) and threw
it on VMS.  There's a problem.

1..12
ok 1 - use Yup;
not ok 2 - lib adding at end of @INC
#     Failed test (lib/lib.t at line 52)
#          got: '../lib'
#     expected: '[.stuff.moo]'
# @INC == [.stuff.moo] ../lib
not ok 3 -     auto/ dir in front of that
#     Failed test (lib/lib.t at line 54)
#          got: '[.stuff.moo]'
#     expected: '[.stuff.moo.VMS_AXP]'
ok 4 -     no duplicates
<snip>

I fed lib.pm a VMS native filepath.  ie:

    use lib '[.stuff.moo]';

and lib.pm isn't catching the existence of the [.stuff.moo.VMS_AXP.auto]
directory.  Its because lib.pm does things like this:

	# 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";

which ain't gonna work.

So there's two ways we can fix this.

    1) Document that lib.pm only takes Unix filepaths.  

Given that:
    @INC will safely take Unix filepaths, even on VMS.  
    %INC always reports its values as Unix filepaths.  
    lib.pm doesn't currently work properly with non-Unix filepaths anyway
        (ie. no backwards compat problem)
    arguments passd to lib.pm are usually fixed strings (ie. its rare
        that you'll have to do programmatic conversion from native to Unix)

that's not too much of a stretch.

    2) Alter lib_pm.PL so on non-Unix platforms lib.pm uses File::Spec.

Which leads to the problem of lib.pm dragging in File::Spec.

The nice part about chosing #1 is if it doesn't work out, we can
always chose #2 later.  However, the reverse does not hold true.


So I'm going with #1.  The test and patch below reflects that.
(Jarkko, disregard the one I sent to you an hour or two ago)


--- lib/lib_pm.PL	2001/12/19 12:37:55	1.1
+++ lib/lib_pm.PL	2001/12/19 12:39:52
@@ -171,6 +171,15 @@
 
     @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 SEE ALSO
 
--- lib/lib.t	2001/12/19 01:13:35	1.1
+++ lib/lib.t	2001/12/19 12:37:24
@@ -0,0 +1,75 @@
+#!./perl -w
+
+BEGIN {
+    chdir 't';
+    @INC = '../lib';
+    @OrigINC = @INC;
+}
+
+use Test::More tests => 12;
+use Config;
+use File::Spec;
+use File::Path;
+
+#set up files and directories
+my @lib_dir;
+my $Lib_Dir;
+my $Arch_Dir;
+my $Auto_Dir;
+my $Module;
+BEGIN {
+    # lib.pm is documented to only work with Unix filepaths.
+    @lib_dir  = qw(stuff moo);
+    $Lib_Dir  = join "/", @lib_dir;
+    $Arch_Dir = join "/", @lib_dir, $Config{archname};
+
+    # create the auto/ directory and a module
+    $Auto_Dir = File::Spec->catdir(@lib_dir, $Config{archname},'auto');
+    $Module   = File::Spec->catfile(@lib_dir, 'Yup.pm');
+
+    mkpath [$Auto_Dir];
+
+    open(MOD, ">$Module") || DIE $!;
+    print MOD <<'MODULE';
+package Yup;
+$Plan = 9;
+return '42';
+MODULE
+
+    close MOD;
+}
+
+END {
+    # cleanup the auto/ directory we created.
+    rmtree([$lib_dir[0]]);
+}
+
+
+use lib $Lib_Dir;
+use lib $Lib_Dir;
+
+BEGIN { use_ok('Yup') }
+
+BEGIN {
+    is( $INC[1], $Lib_Dir,          'lib adding at end of @INC' );
+    print "# \@INC == @INC\n";
+    is( $INC[0], $Arch_Dir,        '    auto/ dir in front of that' );
+    is( grep(/^\Q$Lib_Dir\E$/, @INC), 1,   '    no duplicates' );
+
+    # Yes, %INC uses Unixy filepaths.
+    is( $INC{'Yup.pm'}, join("/",$Lib_Dir, 'Yup.pm'),    '%INC set properly' );
+
+    is( eval { do 'Yup.pm'  }, 42,  'do() works' );
+    ok( eval { require Yup; },      '   require()' );
+    ok( eval "use Yup; 1;",         '   use()' );
+    is( $@, '' );
+
+    is_deeply(\@OrigINC, \@lib::ORIG_INC,    '@lib::ORIG_INC' );
+}
+
+no lib $Lib_Dir;
+
+BEGIN {
+    is( grep(/stuff/, @INC), 0, 'no lib' );
+    ok( !do 'Yup.pm',           '   do() effected' );
+}



-- 

Michael G. Schwern   <schwern@pobox.com>    http://www.pobox.com/~schwern/
Perl Quality Assurance	    <perl-qa@perl.org>	       Kwalitee Is Job One
Let's leave my ass out of this, shall we?

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About