File: wantarray.t

package info (click to toggle)
perl 5.42.0-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 128,392 kB
  • sloc: perl: 534,963; ansic: 240,563; sh: 72,042; pascal: 6,934; xml: 2,428; yacc: 1,360; makefile: 1,197; cpp: 208; lisp: 1
file content (80 lines) | stat: -rw-r--r-- 1,706 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!./perl

BEGIN {
    chdir 't' if -d 't';
    require './test.pl';
    set_up_inc('../lib');
}

use strict;

plan 28;

sub context {
  local $::Level = $::Level + 1;
  my ( $cona, $name ) = @_;
  my $conb = (defined wantarray) ? ( wantarray ? 'A' : 'S' ) : 'V';
  is $conb, $cona, $name;
}

context('V');
my $a = context('S');
my @a = context('A');
scalar context('S');
$a = scalar context('S');
($a) = context('A');
($a) = scalar context('S');

{
  # [ID 20020626.011 (#9998)] incorrect wantarray optimisation
  sub simple { wantarray ? 1 : 2 }
  sub inline {
    my $a = wantarray ? simple() : simple();
    $a;
  }
  my @b = inline();
  my $c = inline();
  is @b, 1;
  is "@b", "2";
  is $c, 2;
}

my $q;

my $qcontext = q{
  $q = (defined wantarray) ? ( wantarray ? 'A' : 'S' ) : 'V';
};
eval $qcontext;
is $q, 'V';
$a = eval $qcontext;
is $q, 'S';
@a = eval $qcontext;
is $q, 'A';

# Test with various ops that the right context is used at the end of a sub-
# routine (run-time context).
$::t = 1;
$::f = 0;
$::u = undef;
sub or_context { $::f || context(shift, "rhs of || at sub exit") }
or_context('V');
$_ = or_context('S');
() = or_context('A');
sub and_context { $::t && context(shift, "rhs of && at sub exit") }
and_context('V');
$_ = and_context('S');
() = and_context('A');
sub dor_context { $::u // context(shift, "rhs of // at sub exit") }
dor_context('V');
$_ = dor_context('S');
() = dor_context('A');
sub cond_middle_cx { $::t ? context(shift, "mid of ?: at sub exit") : 0 }
cond_middle_cx('V');
$_ = cond_middle_cx('S');
() = cond_middle_cx('A');
sub cond_rhs_cx { $::f ? 0 : context(shift, "rhs of ?: at sub exit") }
cond_rhs_cx('V');
$_ = cond_rhs_cx('S');
() = cond_rhs_cx('A');

1;