15 print $ok ? "ok $test - $name\n" : "not ok $test - $name\n";
19 ok( $_ eq 'global', '$_ initial value' );
21 ok( $_ eq 'glabol', 's/// on global $_' );
25 ok( $_ eq 'local', 'my $_ initial value' );
27 ok( $_ eq 'lacol', 's/// on my $_' );
29 ok( $1 eq 'la', '// on my $_' );
30 ok( tr/c/d/ == 1, 'tr/// on my $_ counts correctly' );
31 ok( $_ eq 'ladol', 'tr/// on my $_' );
34 ok( $_ eq 'nested', 'my $_ nested' );
36 ok( $_ eq 'neste', 'chop on my $_' );
40 ok( $_ eq 'glabol', 'gains access to our global $_' );
42 ok( $_ eq 'ladol', 'my $_ restored' );
44 ok( $_ eq 'glabol', 'global $_ restored' );
46 ok( $_ eq 'global', 's/// on global $_ again' );
50 ok( $_ eq 22, 'our $_ is seen explicitly' );
52 ok( $_ eq 2, '...default chop chops our $_' );
54 ok( $1 eq 2, '...default match sees our $_' );
61 ok( $_ eq "foo", 'for my $_' );
63 ok( $1 eq "f", '...m// in for my $_' );
64 ok( our $_ eq 'global', '...our $_ inside for my $_' );
66 ok( $_ eq 'local', '...my $_ restored outside for my $_' );
67 ok( our $_ eq 'global', '...our $_ restored outside for my $_' );
71 for ("implicit foo") { # implicit "my $_"
72 ok( $_ eq "implicit foo", 'for implicit my $_' );
74 ok( $1 eq "i", '...m// in for implicity my $_' );
75 ok( our $_ eq 'global', '...our $_ inside for implicit my $_' );
77 ok( $_ eq 'local', '...my $_ restored outside for implicit my $_' );
78 ok( our $_ eq 'global', '...our $_ restored outside for implicit my $_' );
82 ok( $_ eq "postfix foo", 'postfix for' ) for 'postfix foo';
83 ok( $_ eq 'local', '...my $_ restored outside postfix for' );
84 ok( our $_ eq 'global', '...our $_ restored outside postfix for' );
88 ok( $_ eq "bar", 'for our $_' );
90 ok( $1 eq "b", '...m// in for our $_' );
92 ok( $_ eq 'global', '...our $_ restored outside for our $_' );
97 sub tmap1 { /(.)/; $buf .= $1 } # uses our $_
99 sub tmap2 { /(.)/; $buf .= $1 } # uses my $_
103 ok( /^[67]\z/, 'local lexical $_ is seen in map' );
104 { ok( our $_ eq 'global', 'our $_ still visible' ); }
105 ok( $_ == 6 || $_ == 7, 'local lexical $_ is still seen in map' );
106 { my $_ ; ok( !defined, 'nested my $_ is undefined' ); }
108 ok( $buf eq 'gxgx', q/...map doesn't modify outer lexical $_/ );
109 ok( $_ eq 'x', '...my $_ restored outside map' );
110 ok( our $_ eq 'global', '...our $_ restored outside map' );
111 map { my $_; ok( !defined, 'redeclaring $_ in map block undefs it' ); } 1;
113 { map { my $_; ok( !defined, 'declaring $_ in map block undefs it' ); } 1; }
115 sub tmap3 () { return $_ };
117 sub tmap4 () { return $_ };
118 my $x = join '-', map $_.tmap3.tmap4, 1 .. 2;
119 ok( $x eq '1globallocal-2globallocal', 'map without {}' );
123 my $x = map $_, qw(a b);
124 ok( $x == 2, 'map in scalar context' );
129 sub tgrep1 { /(.)/; $buf .= $1 }
131 sub tgrep2 { /(.)/; $buf .= $1 }
135 ok( /^[89]\z/, 'local lexical $_ is seen in grep' );
136 { ok( our $_ eq 'global', 'our $_ still visible' ); }
137 ok( $_ == 8 || $_ == 9, 'local lexical $_ is still seen in grep' );
139 ok( $buf eq 'gygy', q/...grep doesn't modify outer lexical $_/ );
140 ok( $_ eq 'y', '...my $_ restored outside grep' );
141 ok( our $_ eq 'global', '...our $_ restored outside grep' );
144 sub tgrep3 () { return $_ };
146 sub tgrep4 () { return $_ };
147 my $x = join '-', grep $_=$_.tgrep3.tgrep4, 1 .. 2;
148 ok( $x eq '1globallocal-2globallocal', 'grep without {} with side-effect' );
149 ok( $_ eq 'local', '...but without extraneous side-effects' );
153 my $x = grep $_, qw(a b);
154 ok( $x == 2, 'grep in scalar context' );
160 $s =~ /to(?{ ok( $_ eq 'toto', 'my $_ in code-match # TODO' ) })to/
161 or ok( 0, "\$s=$s should match!" );
162 ok( our $_ eq 'global', '...our $_ restored outside code-match' );
168 ok( $x eq "cba", 'reverse without arguments picks up $_' );
174 ::ok( $::_ eq 'notmain', 'our $_ forced into main::' );
176 ::ok( $1 eq 'notmain', '...m// defaults to our $_ in main::' );
179 my $file = tempfile();
181 open my $_, '>', $file or die "Can't open $file: $!";
184 ok( -s $file, 'writing to filehandle $_ works' );
187 open my $_, $file or die "Can't open $file: $!";
189 ok( $x eq "hello\n", 'reading from <$_> works' );
195 ok( $fqdb::_ eq 'fqdb', 'fully qualified $_ is not in main' );
196 ok( eval q/$fqdb::_/ eq 'fqdb', 'fully qualified, evaled $_ is not in main' );
198 ::ok( $_ ne 'fqdb', 'unqualified $_ is in main' );
199 ::ok( q/$_/ ne 'fqdb', 'unqualified, evaled $_ is in main' );