| 1 |
#!./perl |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use List::Util qw(first); |
| 7 |
use Test::More; |
| 8 |
plan tests => 24; |
| 9 |
my $v; |
| 10 |
|
| 11 |
ok(defined &first, 'defined'); |
| 12 |
|
| 13 |
$v = first { 8 == ($_ - 1) } 9,4,5,6; |
| 14 |
is($v, 9, 'one more than 8'); |
| 15 |
|
| 16 |
$v = first { 0 } 1,2,3,4; |
| 17 |
is($v, undef, 'none match'); |
| 18 |
|
| 19 |
$v = first { 0 }; |
| 20 |
is($v, undef, 'no args'); |
| 21 |
|
| 22 |
$v = first { $_->[1] le "e" and "e" le $_->[2] } |
| 23 |
[qw(a b c)], [qw(d e f)], [qw(g h i)]; |
| 24 |
is_deeply($v, [qw(d e f)], 'reference args'); |
| 25 |
|
| 26 |
# Check that eval{} inside the block works correctly |
| 27 |
my $i = 0; |
| 28 |
$v = first { eval { die }; ($i == 5, $i = $_)[0] } 0,1,2,3,4,5,5; |
| 29 |
is($v, 5, 'use of eval'); |
| 30 |
|
| 31 |
$v = eval { first { die if $_ } 0,0,1 }; |
| 32 |
is($v, undef, 'use of die'); |
| 33 |
|
| 34 |
sub foobar { first { !defined(wantarray) || wantarray } "not ","not ","not " } |
| 35 |
|
| 36 |
($v) = foobar(); |
| 37 |
is($v, undef, 'wantarray'); |
| 38 |
|
| 39 |
# Can we leave the sub with 'return'? |
| 40 |
$v = first {return ($_>6)} 2,4,6,12; |
| 41 |
is($v, 12, 'return'); |
| 42 |
|
| 43 |
# ... even in a loop? |
| 44 |
$v = first {while(1) {return ($_>6)} } 2,4,6,12; |
| 45 |
is($v, 12, 'return from loop'); |
| 46 |
|
| 47 |
# Does it work from another package? |
| 48 |
{ package Foo; |
| 49 |
::is(List::Util::first(sub{$_>4},(1..4,24)), 24, 'other package'); |
| 50 |
} |
| 51 |
|
| 52 |
# Can we undefine a first sub while it's running? |
| 53 |
sub self_immolate {undef &self_immolate; 1} |
| 54 |
eval { $v = first \&self_immolate, 1,2; }; |
| 55 |
like($@, qr/^Can't undef active subroutine/, "undef active sub"); |
| 56 |
|
| 57 |
# Redefining an active sub should not fail, but whether the |
| 58 |
# redefinition takes effect immediately depends on whether we're |
| 59 |
# running the Perl or XS implementation. |
| 60 |
|
| 61 |
sub self_updating { |
| 62 |
no warnings 'redefine'; |
| 63 |
*self_updating = sub{1}; |
| 64 |
1 |
| 65 |
} |
| 66 |
eval { $v = first \&self_updating, 1,2; }; |
| 67 |
is($@, '', 'redefine self'); |
| 68 |
|
| 69 |
{ my $failed = 0; |
| 70 |
|
| 71 |
sub rec { my $n = shift; |
| 72 |
if (!defined($n)) { # No arg means we're being called by first() |
| 73 |
return 1; } |
| 74 |
if ($n<5) { rec($n+1); } |
| 75 |
else { $v = first \&rec, 1,2; } |
| 76 |
$failed = 1 if !defined $n; |
| 77 |
} |
| 78 |
|
| 79 |
rec(1); |
| 80 |
ok(!$failed, 'from active sub'); |
| 81 |
} |
| 82 |
|
| 83 |
# Calling a sub from first should leave its refcount unchanged. |
| 84 |
SKIP: { |
| 85 |
skip("No Internals::SvREFCNT", 1) if !defined &Internals::SvREFCNT; |
| 86 |
sub huge {$_>1E6} |
| 87 |
my $refcnt = &Internals::SvREFCNT(\&huge); |
| 88 |
$v = first \&huge, 1..6; |
| 89 |
is(&Internals::SvREFCNT(\&huge), $refcnt, "Refcount unchanged"); |
| 90 |
} |
| 91 |
|
| 92 |
# These tests are only relevant for the real multicall implementation. The |
| 93 |
# pseudo-multicall implementation behaves differently. |
| 94 |
SKIP: { |
| 95 |
$List::Util::REAL_MULTICALL ||= 0; # Avoid use only once |
| 96 |
skip("Poor man's MULTICALL can't cope", 2) |
| 97 |
if !$List::Util::REAL_MULTICALL; |
| 98 |
|
| 99 |
# Can we goto a label from the 'first' sub? |
| 100 |
eval {()=first{goto foo} 1,2; foo: 1}; |
| 101 |
like($@, qr/^Can't "goto" out of a pseudo block/, "goto label"); |
| 102 |
|
| 103 |
# Can we goto a subroutine? |
| 104 |
eval {()=first{goto sub{}} 1,2;}; |
| 105 |
like($@, qr/^Can't goto subroutine from a sort sub/, "goto sub"); |
| 106 |
} |
| 107 |
|
| 108 |
use constant XSUBC_TRUE => 1; |
| 109 |
use constant XSUBC_FALSE => 0; |
| 110 |
|
| 111 |
is first(\&XSUBC_TRUE, 42, 1, 2, 3), 42, 'XSUB callbacks'; |
| 112 |
is first(\&XSUBC_FALSE, 42, 1, 2, 3), undef, 'XSUB callbacks'; |
| 113 |
|
| 114 |
|
| 115 |
eval { &first(1) }; |
| 116 |
ok($@ =~ /^Not a subroutine reference/, 'check for code reference'); |
| 117 |
eval { &first(1,2) }; |
| 118 |
ok($@ =~ /^Not a subroutine reference/, 'check for code reference'); |
| 119 |
eval { &first(qw(a b)) }; |
| 120 |
ok($@ =~ /^Not a subroutine reference/, 'check for code reference'); |
| 121 |
eval { &first([],1,2,3) }; |
| 122 |
ok($@ =~ /^Not a subroutine reference/, 'check for code reference'); |
| 123 |
eval { &first(+{},1,2,3) }; |
| 124 |
ok($@ =~ /^Not a subroutine reference/, 'check for code reference'); |
| 125 |
|