1#!./perl
2
3BEGIN {
4    chdir 't' if -d 't';
5    require './test.pl';
6    set_up_inc('../lib');
7}
8
9use strict;
10
11plan 28;
12
13sub context {
14  local $::Level = $::Level + 1;
15  my ( $cona, $name ) = @_;
16  my $conb = (defined wantarray) ? ( wantarray ? 'A' : 'S' ) : 'V';
17  is $conb, $cona, $name;
18}
19
20context('V');
21my $a = context('S');
22my @a = context('A');
23scalar context('S');
24$a = scalar context('S');
25($a) = context('A');
26($a) = scalar context('S');
27
28{
29  # [ID 20020626.011 (#9998)] incorrect wantarray optimisation
30  sub simple { wantarray ? 1 : 2 }
31  sub inline {
32    my $a = wantarray ? simple() : simple();
33    $a;
34  }
35  my @b = inline();
36  my $c = inline();
37  is @b, 1;
38  is "@b", "2";
39  is $c, 2;
40}
41
42my $q;
43
44my $qcontext = q{
45  $q = (defined wantarray) ? ( wantarray ? 'A' : 'S' ) : 'V';
46};
47eval $qcontext;
48is $q, 'V';
49$a = eval $qcontext;
50is $q, 'S';
51@a = eval $qcontext;
52is $q, 'A';
53
54# Test with various ops that the right context is used at the end of a sub-
55# routine (run-time context).
56$::t = 1;
57$::f = 0;
58$::u = undef;
59sub or_context { $::f || context(shift, "rhs of || at sub exit") }
60or_context('V');
61$_ = or_context('S');
62() = or_context('A');
63sub and_context { $::t && context(shift, "rhs of && at sub exit") }
64and_context('V');
65$_ = and_context('S');
66() = and_context('A');
67sub dor_context { $::u // context(shift, "rhs of // at sub exit") }
68dor_context('V');
69$_ = dor_context('S');
70() = dor_context('A');
71sub cond_middle_cx { $::t ? context(shift, "mid of ?: at sub exit") : 0 }
72cond_middle_cx('V');
73$_ = cond_middle_cx('S');
74() = cond_middle_cx('A');
75sub cond_rhs_cx { $::f ? 0 : context(shift, "rhs of ?: at sub exit") }
76cond_rhs_cx('V');
77$_ = cond_rhs_cx('S');
78() = cond_rhs_cx('A');
79
801;
81