This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Spelling correction for consistency with pod/perldebguts.pod.
[perl5.git] / t / io / nargv.t
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     require "./test.pl";
6     set_up_inc('../lib');
7 }
8
9 print "1..6\n";
10
11 my $j = 1;
12 for $i ( 1,2,5,4,3 ) {
13     $file = mkfiles($i);
14     open(FH, "> $file") || die "can't create $file: $!";
15     print FH "not ok " . $j++ . "\n";
16     close(FH) || die "Can't close $file: $!";
17 }
18
19
20 {
21     local *ARGV;
22     local $^I = '.bak';
23     local $_;
24     @ARGV = mkfiles(1..3);
25     $n = 0;
26     while (<>) {
27         print STDOUT "# initial \@ARGV: [@ARGV]\n";
28         if ($n++ == 2) {
29             other();
30         }
31         show();
32     }
33 }
34
35 $^I = undef;
36 @ARGV = mkfiles(1..3);
37 $n = 0;
38 while (<>) {
39     print STDOUT "#final \@ARGV: [@ARGV]\n";
40     if ($n++ == 2) {
41         other();
42     }
43     show();
44 }
45
46 # test setuid is preserved (and hopefully setgid)
47 #
48 # With nested in-place editing PL_oldname and PL_filemode would
49 # be overwritten by the values for the last file in the nested
50 # loop.  This is now all stored as magic in *ARGVOUT{IO}
51 $^I = "";
52 @ARGV = mkfiles(1..3);
53 my $sidfile = $ARGV[1];
54 chmod(04600, $sidfile);
55 my $mode = (stat $ARGV[1])[2];
56 $n = 0;
57 while (<>) {
58     print STDOUT "#final \@ARGV: [@ARGV]\n";
59     if ($n++ == 1) {
60         other();
61     }
62     print;
63 }
64 my $newmode = (stat $sidfile)[2];
65 printf "# before %#o after %#o\n", $mode, $newmode;
66 print +($mode == $newmode ? "" : "not "). "ok 6 # check setuid mode preserved\n";
67
68 sub show {
69     #warn "$ARGV: $_";
70     s/^not //;
71     print;
72 }
73
74 sub other {
75     no warnings 'once';
76     print STDOUT "# Calling other\n";
77     local *ARGV;
78     local *ARGVOUT;
79     local $_;
80     @ARGV = mkfiles(5, 4);
81     while (<>) {
82         print STDOUT "# inner \@ARGV: [@ARGV]\n";
83         show();
84     }
85 }
86
87 my @files;
88 sub mkfiles {
89     foreach (@_) {
90         $files[$_] ||= tempfile();
91     }
92     my @results = @files[@_];
93     return wantarray ? @results : @results[-1];
94 }
95
96 END { unlink_all map { ($_, "$_.bak") } mkfiles(1..5) }