7 require Config; import Config;
8 keys %Config; # Silence warning
9 if ($Config{extensions} !~ /\bList\/Util\b/) {
10 print "1..0 # Skip: List::Util was not built\n";
16 use List::Util qw(first);
18 plan tests => 22 + ($::PERL_ONLY ? 0 : 2);
21 ok(defined &first, 'defined');
23 $v = first { 8 == ($_ - 1) } 9,4,5,6;
24 is($v, 9, 'one more than 8');
26 $v = first { 0 } 1,2,3,4;
27 is($v, undef, 'none match');
30 is($v, undef, 'no args');
32 $v = first { $_->[1] le "e" and "e" le $_->[2] }
33 [qw(a b c)], [qw(d e f)], [qw(g h i)];
34 is_deeply($v, [qw(d e f)], 'reference args');
36 # Check that eval{} inside the block works correctly
38 $v = first { eval { die }; ($i == 5, $i = $_)[0] } 0,1,2,3,4,5,5;
39 is($v, 5, 'use of eval');
41 $v = eval { first { die if $_ } 0,0,1 };
42 is($v, undef, 'use of die');
44 sub foobar { first { !defined(wantarray) || wantarray } "not ","not ","not " }
47 is($v, undef, 'wantarray');
49 # Can we leave the sub with 'return'?
50 $v = first {return ($_>6)} 2,4,6,12;
54 $v = first {while(1) {return ($_>6)} } 2,4,6,12;
55 is($v, 12, 'return from loop');
57 # Does it work from another package?
59 ::is(List::Util::first(sub{$_>4},(1..4,24)), 24, 'other package');
62 # Can we undefine a first sub while it's running?
63 sub self_immolate {undef &self_immolate; 1}
64 eval { $v = first \&self_immolate, 1,2; };
65 like($@, qr/^Can't undef active subroutine/, "undef active sub");
67 # Redefining an active sub should not fail, but whether the
68 # redefinition takes effect immediately depends on whether we're
69 # running the Perl or XS implementation.
71 sub self_updating { local $^W; *self_updating = sub{1} ;1}
72 eval { $v = first \&self_updating, 1,2; };
73 is($@, '', 'redefine self');
77 sub rec { my $n = shift;
78 if (!defined($n)) { # No arg means we're being called by first()
80 if ($n<5) { rec($n+1); }
81 else { $v = first \&rec, 1,2; }
82 $failed = 1 if !defined $n;
86 ok(!$failed, 'from active sub');
89 # Calling a sub from first should leave its refcount unchanged.
91 skip("No Internals::SvREFCNT", 1) if !defined &Internals::SvREFCNT;
93 my $refcnt = &Internals::SvREFCNT(\&huge);
94 $v = first \&huge, 1..6;
95 is(&Internals::SvREFCNT(\&huge), $refcnt, "Refcount unchanged");
98 # The remainder of the tests are only relevant for the XS
99 # implementation. The Perl-only implementation behaves differently
100 # (and more flexibly) in a way that we can't emulate from XS.
101 if (!$::PERL_ONLY) { SKIP: {
103 $List::Util::REAL_MULTICALL ||= 0; # Avoid use only once
104 skip("Poor man's MULTICALL can't cope", 2)
105 if !$List::Util::REAL_MULTICALL;
107 # Can we goto a label from the 'first' sub?
108 eval {()=first{goto foo} 1,2; foo: 1};
109 like($@, qr/^Can't "goto" out of a pseudo block/, "goto label");
111 # Can we goto a subroutine?
112 eval {()=first{goto sub{}} 1,2;};
113 like($@, qr/^Can't goto subroutine from a sort sub/, "goto sub");
117 use constant XSUBC_TRUE => 1;
118 use constant XSUBC_FALSE => 0;
120 is first(\&XSUBC_TRUE, 42, 1, 2, 3), 42, 'XSUB callbacks';
121 is first(\&XSUBC_FALSE, 42, 1, 2, 3), undef, 'XSUB callbacks';
125 ok($@ =~ /^Not a subroutine reference/, 'check for code reference');
126 eval { &first(1,2) };
127 ok($@ =~ /^Not a subroutine reference/, 'check for code reference');
128 eval { &first(qw(a b)) };
129 ok($@ =~ /^Not a subroutine reference/, 'check for code reference');
130 eval { &first([],1,2,3) };
131 ok($@ =~ /^Not a subroutine reference/, 'check for code reference');
132 eval { &first(+{},1,2,3) };
133 ok($@ =~ /^Not a subroutine reference/, 'check for code reference');