xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/t/op/my.t (revision 0:68f95e015346)
1#!./perl
2
3# $RCSfile: my.t,v $
4
5print "1..33\n";
6
7sub foo {
8    my($a, $b) = @_;
9    my $c;
10    my $d;
11    $c = "ok 3\n";
12    $d = "ok 4\n";
13    { my($a, undef, $c) = ("ok 9\n", "not ok 10\n", "ok 10\n");
14      ($x, $y) = ($a, $c); }
15    print $a, $b;
16    $c . $d;
17}
18
19$a = "ok 5\n";
20$b = "ok 6\n";
21$c = "ok 7\n";
22$d = "ok 8\n";
23
24print &foo("ok 1\n","ok 2\n");
25
26print $a,$b,$c,$d,$x,$y;
27
28# same thing, only with arrays and associative arrays
29
30sub foo2 {
31    my($a, @b) = @_;
32    my(@c, %d);
33    @c = "ok 13\n";
34    $d{''} = "ok 14\n";
35    { my($a,@c) = ("ok 19\n", "ok 20\n"); ($x, $y) = ($a, @c); }
36    print $a, @b;
37    $c[0] . $d{''};
38}
39
40$a = "ok 15\n";
41@b = "ok 16\n";
42@c = "ok 17\n";
43$d{''} = "ok 18\n";
44
45print &foo2("ok 11\n","ok 12\n");
46
47print $a,@b,@c,%d,$x,$y;
48
49my $i = "outer";
50
51if (my $i = "inner") {
52    print "not " if $i ne "inner";
53}
54print "ok 21\n";
55
56if ((my $i = 1) == 0) {
57    print "not ";
58}
59else {
60    print "not" if $i != 1;
61}
62print "ok 22\n";
63
64my $j = 5;
65while (my $i = --$j) {
66    print("not "), last unless $i > 0;
67}
68continue {
69    print("not "), last unless $i > 0;
70}
71print "ok 23\n";
72
73$j = 5;
74for (my $i = 0; (my $k = $i) < $j; ++$i) {
75    print("not "), last unless $i >= 0 && $i < $j && $i == $k;
76}
77print "ok 24\n";
78print "not " if defined $k;
79print "ok 25\n";
80
81foreach my $i (26, 27) {
82    print "ok $i\n";
83}
84
85print "not " if $i ne "outer";
86print "ok 28\n";
87
88# Ensure that C<my @y> (without parens) doesn't force scalar context.
89my @x;
90{ @x = my @y }
91print +(@x ? "not " : ""), "ok 29\n";
92{ @x = my %y }
93print +(@x ? "not " : ""), "ok 30\n";
94
95# Found in HTML::FormatPS
96my %fonts = qw(nok 31);
97for my $full (keys %fonts) {
98    $full =~ s/^n//;
99    # Supposed to be copy-on-write via force_normal after a THINKFIRST check.
100    print "$full $fonts{nok}\n";
101}
102
103#  [perl #29340] optimising away the = () left the padav returning the
104# array rather than the contents, leading to 'Bizarre copy of array' error
105
106sub opta { my @a=() }
107sub opth { my %h=() }
108eval { my $x = opta };
109print "not " if $@;
110print "ok 32\n";
111eval { my $x = opth };
112print "not " if $@;
113print "ok 33\n";
114