[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]

[NEW TEST t/op/our.t 5.005_63] scope tests for our()



I noticed that our() had no test script.  I copyed my()'s test script,
since they're supposed to have the same scoping rules, and pretty much
just s/my/our/g and ran it.  That's how I noticed the last few bugs I
reported.

So, here is a basic t/op/our.t that tests its scoping rules.  Like I
said, its just t/op/my.t with our() in its place.  Some our() specific
tests need to be added (checking to make sure if fully qualified
variable names work, for instance), but this is a good start.

BTW Tests 1 through 18 are all screwed up due to the scoping bug and
tests #25 and #28 because of the failure of our() to scope right
inside a loop conditional.  Additionally there are the spurious
'Useless use of a variable' warnings.  (The "Found = in conditional"
is expected)  You should see something like this:


$ ./perl -w op/our.t 
Useless use of a variable in void context at op/our.t line 9.
Useless use of a variable in void context at op/our.t line 10.
Found = in conditional, should be == at op/our.t line 51.
Useless use of a variable in void context at op/our.t line 89.
1..30
ok 9
ok 2
ok 10
ok 4
ok 9
ok 2
ok 10
ok 4
ok 9
ok 10
ok 19
ok 12
ok 20
ok 14
ok 19
ok 12
ok 20
ok 14
ok 19
ok 20
ok 21
ok 22
ok 23
ok 24
not ok 25
ok 26
ok 27
not ok 28
ok 29
ok 30

The test is right, our() is wrong (see my last two bug reports).  Or
the our() docs are wrong.

t/op/our.t is given here:


#!./perl

# $RCSfile: our.t,v $

print "1..30\n";

sub foo {
    our($a, $b) = @_;
    our $c;
    our $d;
    $c = "ok 3\n";
    $d = "ok 4\n";
    { our($a, undef, $c) = ("ok 9\n", "not ok 10\n", "ok 10\n");
      ($x, $y) = ($a, $c); }
    print $a, $b;
    $c . $d;
}

$a = "ok 5\n";
$b = "ok 6\n";
$c = "ok 7\n";
$d = "ok 8\n";

print &foo("ok 1\n","ok 2\n");

print $a,$b,$c,$d,$x,$y;

# same thing, only with arrays and associative arrays

sub foo2 {
    our($a, @b) = @_;
    our(@c, %d);
    @c = "ok 13\n";
    $d{''} = "ok 14\n";
    { our($a,@c) = ("ok 19\n", "ok 20\n"); ($x, $y) = ($a, @c); }
    print $a, @b;
    $c[0] . $d{''};
}

$a = "ok 15\n";
@b = "ok 16\n";
@c = "ok 17\n";
$d{''} = "ok 18\n";

print &foo2("ok 11\n","ok 12\n");

print $a,@b,@c,%d,$x,$y;

our $i = "outer";

if (our $i = "inner") {
    print "not " if $i ne "inner";
}
print "ok 21\n";

if ((our $i = 1) == 0) {
    print "not ";
}
else {
    print "not" if $i != 1;
}
print "ok 22\n";

our $j = 5;
while (our $i = --$j) {
    print("not "), last unless $i > 0;
}
continue {
    print("not "), last unless $i > 0;
}
print "ok 23\n";

$j = 5;
for (our $i = 0; (our $k = $i) < $j; ++$i) {
    print("not "), last unless $i >= 0 && $i < $j && $i == $k;
}
print "ok 24\n";
print "not " if defined $k;
print "ok 25\n";

foreach our $i (26, 27) {
    print "ok $i\n";
}

print "not " if $i ne "outer";
print "ok 28\n";

# Ensure that C<our @y> (without parens) doesn't force scalar context.
our @x;
{ @x = our @y }
print +(@x ? "not " : ""), "ok 29\n";
{ @x = our %y }
print +(@x ? "not " : ""), "ok 30\n";



-- 

Michael G Schwern                                           schwern@pobox.com
                    http://www.pobox.com/~schwern
     /(?:(?:(1)[.-]?)?\(?(\d{3})\)?[.-]?)?(\d{3})[.-]?(\d{4})(x\d+)?/i


Follow-Ups from:
Gurusamy Sarathy <gsar@ActiveState.com>

[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]