1#!./perl 2BEGIN { 3 chdir 't' if -d 't'; 4 require './test.pl'; 5 set_up_inc('../lib'); 6} 7 8sub foo { 9 my($a, $b) = @_; 10 my $c; 11 my $d; 12 $c = "ok 3\n"; 13 $d = "ok 4\n"; 14 { my($a, undef, $c) = ("ok 9\n", "not ok 10\n", "ok 10\n"); 15 ($x, $y) = ($a, $c); } 16 is($a, "ok 1\n", 'value of sub argument maintained outside of block'); 17 is($b, "ok 2\n", 'sub argument maintained'); 18 is($c, "ok 3\n", 'variable value maintained outside of block'); 19 is($d, "ok 4\n", 'variable value maintained'); 20} 21 22$a = "ok 5\n"; 23$b = "ok 6\n"; 24$c = "ok 7\n"; 25$d = "ok 8\n"; 26 27&foo("ok 1\n","ok 2\n"); 28 29is($a, "ok 5\n", 'global was not affected by duplicate names inside subroutine'); 30is($b, "ok 6\n", '...'); 31is($c, "ok 7\n", '...'); 32is($d, "ok 8\n", '...'); 33is($x, "ok 9\n", 'globals modified inside of block keeps its value outside of block'); 34is($y, "ok 10\n", '...'); 35 36# same thing, only with arrays and associative arrays 37 38sub foo2 { 39 my($a, @b) = @_; 40 my(@c, %d); 41 @c = "ok 13\n"; 42 $d{''} = "ok 14\n"; 43 { my($a,@c) = ("ok 19\n", "ok 20\n", "ok 21\n"); ($x, $y) = ($a, @c); } 44 is($a, "ok 11\n", 'value of sub argument maintained outside of block'); 45 is(scalar @b, 1, 'did not add any elements to @b'); 46 is($b[0], "ok 12\n", 'did not alter @b'); 47 is(scalar @c, 1, 'did not add arguments to @c'); 48 is($c[0], "ok 13\n", 'did not alter @c'); 49 is($d{''}, "ok 14\n", 'did not touch %d'); 50} 51 52$a = "ok 15\n"; 53@b = "ok 16\n"; 54@c = "ok 17\n"; 55$d{''} = "ok 18\n"; 56 57&foo2("ok 11\n", "ok 12\n"); 58 59is($a, "ok 15\n", 'Global was not modifed out of scope'); 60is(scalar @b, 1, 'correct number of elements in array'); 61is($b[0], "ok 16\n", 'array value was not modified out of scope'); 62is(scalar @c, 1, 'correct number of elements in array'); 63is($c[0], "ok 17\n", 'array value was not modified out of scope'); 64is($d{''}, "ok 18\n", 'hash key/value pair is correct'); 65is($x, "ok 19\n", 'global was modified'); 66is($y, "ok 20\n", 'this one too'); 67 68my $i = "outer"; 69 70if (my $i = "inner") { 71 is( $i, 'inner', 'my variable inside conditional propagates inside block'); 72} 73 74if ((my $i = 1) == 0) { 75 fail("nested parens do not propagate variable outside"); 76} 77else { 78 is($i, 1, 'lexical variable lives available inside else block'); 79} 80 81my $j = 5; 82while (my $i = --$j) { 83 last unless is( $i, $j, 'lexical inside while block'); 84} 85continue { 86 last unless is( $i, $j, 'lexical inside continue block'); 87} 88is( $j, 0, 'went through the previous while/continue loop all 4 times' ); 89 90$j = 5; 91for (my $i = 0; (my $k = $i) < $j; ++$i) { 92 fail(""), last unless $i >= 0 && $i < $j && $i == $k; 93} 94ok( ! defined $k, '$k is only defined in the scope of the previous for loop' ); 95 96curr_test(37); 97$jj = 0; 98foreach my $i (30, 31) { 99 is( $i, $jj+30, 'assignment inside the foreach loop variable definition'); 100 $jj++; 101} 102is( $jj, 2, 'foreach loop executed twice'); 103 104is( $i, 'outer', '$i not modified by while/for/foreach using same variable name'); 105 106# Ensure that C<my @y> (without parens) doesn't force scalar context. 107my @x; 108{ @x = my @y } 109is(scalar @x, 0, 'my @y without parens does not force scalar context'); 110{ @x = my %y } 111is(scalar @x, 0, 'my %y without parens does not force scalar context'); 112 113# Found in HTML::FormatPS 114my %fonts = qw(nok 35); 115for my $full (keys %fonts) { 116 $full =~ s/^n//; 117 is( $fonts{nok}, 35, 'Supposed to be copy-on-write via force_normal after a THINKFIRST check.' ); 118} 119 120# [perl #29340] optimising away the = () left the padav returning the 121# array rather than the contents, leading to 'Bizarre copy of array' error 122 123sub opta { my @a=() } 124sub opth { my %h=() } 125eval { my $x = opta }; 126is($@, '', ' perl #29340, No bizarre copy of array error'); 127eval { my $x = opth }; 128is($@, '', ' perl #29340, No bizarre copy of array error via hash'); 129 130sub foo3 { 131 ++my $x->{foo}; 132 ok(! defined $x->{bar}, '$x->{bar} is not defined'); 133 ++$x->{bar}; 134} 135eval { foo3(); foo3(); }; 136is( $@, '', 'no errors while checking autovivification and persistence of hash refs inside subs' ); 137 138# my $foo = undef should always assign [perl #37776] 139{ 140 my $count = 35; 141 loop: 142 my $test = undef; 143 is($test, undef, 'var is undef, repeated test'); 144 $test = 42; 145 goto loop if ++$count < 37; 146} 147 148# [perl #113554] 149eval "my ()"; 150is( $@, '', "eval of my() passes"); 151 152# RT #126844 153# This triggered a compile-time assert failure in rpeep() 154eval 'my($a,$b),$x,my($c,$d)'; 155pass("RT #126844"); 156 157# RT # 133543 158my @false_conditionals = ( 159 'my $x1 if 0;', 160 'my @x2 if 0;', 161 'my %x3 if 0;', 162 'my ($x4) if 0;', 163 'my ($x5,@x6, %x7) if 0;', 164 '0 && my $z1;', 165 '0 && my (%z2);', 166); 167for (my $i=0; $i<=$#false_conditionals; $i++) { 168 eval $false_conditionals[$i]; 169 like( $@, qr/^This use of my\(\) in false conditional is no longer allowed/, 170 "RT #133543: my() in false conditional: $false_conditionals[$i]"); 171} 172 173#Variable number of tests due to the way the while/for loops are tested now 174done_testing(); 175