This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #22236] File::Basename behavior is misleading
[perl5.git] / lib / Devel / SelfStubber.pm
index ba833ff..bfdb443 100644 (file)
@@ -1,4 +1,5 @@
 package Devel::SelfStubber;
+use File::Spec;
 require SelfLoader;
 @ISA = qw(SelfLoader);
 @EXPORT = 'AUTOLOAD';
@@ -28,13 +29,14 @@ sub _package_defined {
 
 sub stub {
     my($self,$module,$lib) = @_;
-    my($line,$end,$fh,$mod_file,$found_selfloader);
-    $lib ||= '.';
+    my($line,$end_data,$fh,$mod_file,$found_selfloader);
+    $lib ||= File::Spec->curdir();
     ($mod_file = $module) =~ s,::,/,g;
+    $mod_file =~ tr|/|:| if $^O eq 'MacOS';
     
-    $mod_file = "$lib/$mod_file.pm";
+    $mod_file = File::Spec->catfile($lib, "$mod_file.pm");
     $fh = "${module}::DATA";
-    my (@BEFORE_DATA, @AFTER_DATA);
+    my (@BEFORE_DATA, @AFTER_DATA, @AFTER_END);
     @DATA = @STUBS = ();
 
     open($fh,$mod_file) || die "Unable to open $mod_file";
@@ -47,20 +49,26 @@ sub stub {
       || die "$mod_file doesn't contain a __DATA__ token";
     $found_selfloader || 
        print 'die "\'use SelfLoader;\' statement NOT FOUND!!\n"',"\n";
-    $self->_load_stubs($module);
+    if ($JUST_STUBS) {
+        $self->_load_stubs($module);
+    } else {
+        $self->_load_stubs($module, \@AFTER_END);
+    }
     if ( fileno($fh) ) {
-       $end = 1;
+       $end_data = 1;
        while(defined($line = <$fh>)) {
            push(@AFTER_DATA,$line);
        }
     }
+    close($fh);
     unless ($JUST_STUBS) {
        print @BEFORE_DATA;
     }
     print @STUBS;
     unless ($JUST_STUBS) {
        print "1;\n__DATA__\n",@DATA;
-       if($end) { print "__END__\n",@AFTER_DATA; }
+       if($end_data) { print "__END__ DATA\n",@AFTER_DATA; }
+       if(@AFTER_END) { print "__END__\n",@AFTER_END; }
     }
 }