| 1 | # Copyright (c) 1997-2009 Graham Barr <gbarr@pobox.com>. All rights reserved. | ||
| 2 | # This program is free software; you can redistribute it and/or | ||
| 3 | # modify it under the same terms as Perl itself. | ||
| 4 | # | ||
| 5 | # Maintained since 2013 by Paul Evans <leonerd@leonerd.org.uk> | ||
| 6 | # Since 2015 unauthorized by Reini Urban for cperl (binary names, utf8, lexical $_, ...) | ||
| 7 | |||
| 8 | package List::Util; | ||
| 9 | |||
| 10 | use strict; | ||
| 11 | use warnings; | ||
| 12 | require Exporter; | ||
| 13 | |||
| 14 | our @ISA = qw(Exporter); | ||
| 15 | our @EXPORT_OK = qw( | ||
| 16 | all any first min max minstr maxstr none notall product reduce sum sum0 shuffle uniq uniqnum uniqstr | ||
| 17 | head tail pairs unpairs pairkeys pairvalues pairmap pairgrep pairfirst | ||
| 18 | ); | ||
| 19 | our $VERSION = "1.50_11"; | ||
| 20 | our $XS_VERSION = $VERSION; | ||
| 21 | $VERSION = eval $VERSION; | ||
| 22 | |||
| 23 | require XSLoader; | ||
| 24 | XSLoader::load('List::Util', $XS_VERSION); | ||
| 25 | |||
| 26 | sub import | ||
| 27 | { | ||
| 28 | my $pkg = caller; | ||
| 29 | |||
| 30 | # (RT88848) Touch the caller's $a and $b, to avoid the warning of | ||
| 31 | # Name "main::a" used only once: possible typo" warning | ||
| 32 | no strict 'refs'; | ||
| 33 | ${"${pkg}::a"} = ${"${pkg}::a"}; | ||
| 34 | ${"${pkg}::b"} = ${"${pkg}::b"}; | ||
| 35 | |||
| 36 | goto &Exporter::import; | ||
| 37 | } | ||
| 38 | |||
| 39 | # For objects returned by pairs() | ||
| 40 | sub List::Util::_Pair::key { shift->[0] } | ||
| 41 | sub List::Util::_Pair::value { shift->[1] } | ||
| 42 | |||
| 43 | =head1 NAME | ||
| 44 | |||
| 45 | List::Util - A selection of general-utility list subroutines | ||
| 46 | |||
| 47 | =head1 SYNOPSIS | ||
| 48 | |||
| 49 | use List::Util qw( | ||
| 50 | reduce any all none notall first | ||
| 51 | |||
| 52 | max maxstr min minstr product sum sum0 | ||
| 53 | |||
| 54 | pairs unpairs pairkeys pairvalues pairfirst pairgrep pairmap | ||
| 55 | |||
| 56 | shuffle uniq uniqnum uniqstr | ||
| 57 | ); | ||
| 58 | |||
| 59 | =head1 DESCRIPTION | ||
| 60 | |||
| 61 | C<List::Util> contains a selection of subroutines that people have expressed | ||
| 62 | would be nice to have in the perl core, but the usage would not really be high | ||
| 63 | enough to warrant the use of a keyword, and the size so small such that being | ||
| 64 | individual extensions would be wasteful. | ||
| 65 | |||
| 66 | By default C<List::Util> does not export any subroutines. | ||
| 67 | |||
| 68 | =cut | ||
| 69 | |||
| 70 | =head1 LIST-REDUCTION FUNCTIONS | ||
| 71 | |||
| 72 | The following set of functions all reduce a list down to a single value. | ||
| 73 | |||
| 74 | =cut | ||
| 75 | |||
| 76 | =head2 reduce | ||
| 77 | |||
| 78 | $result = reduce { BLOCK } @list | ||
| 79 | |||
| 80 | Reduces C<@list> by calling C<BLOCK> in a scalar context multiple times, | ||
| 81 | setting C<$a> and C<$b> each time. The first call will be with C<$a> and C<$b> | ||
| 82 | set to the first two elements of the list, subsequent calls will be done by | ||
| 83 | setting C<$a> to the result of the previous call and C<$b> to the next element | ||
| 84 | in the list. | ||
| 85 | |||
| 86 | Returns the result of the last call to the C<BLOCK>. If C<@list> is empty then | ||
| 87 | C<undef> is returned. If C<@list> only contains one element then that element | ||
| 88 | is returned and C<BLOCK> is not executed. | ||
| 89 | |||
| 90 | The following examples all demonstrate how C<reduce> could be used to implement | ||
| 91 | the other list-reduction functions in this module. (They are not in fact | ||
| 92 | implemented like this, but instead in a more efficient manner in individual C | ||
| 93 | functions). | ||
| 94 | |||
| 95 | $foo = reduce { defined($a) ? $a : | ||
| 96 | $code->(local $_ = $b) ? $b : | ||
| 97 | undef } undef, @list # first | ||
| 98 | |||
| 99 | $foo = reduce { $a > $b ? $a : $b } 1..10 # max | ||
| 100 | $foo = reduce { $a gt $b ? $a : $b } 'A'..'Z' # maxstr | ||
| 101 | $foo = reduce { $a < $b ? $a : $b } 1..10 # min | ||
| 102 | $foo = reduce { $a lt $b ? $a : $b } 'aa'..'zz' # minstr | ||
| 103 | $foo = reduce { $a + $b } 1 .. 10 # sum | ||
| 104 | $foo = reduce { $a . $b } @bar # concat | ||
| 105 | |||
| 106 | $foo = reduce { $a || $code->(local $_ = $b) } 0, @bar # any | ||
| 107 | $foo = reduce { $a && $code->(local $_ = $b) } 1, @bar # all | ||
| 108 | $foo = reduce { $a && !$code->(local $_ = $b) } 1, @bar # none | ||
| 109 | $foo = reduce { $a || !$code->(local $_ = $b) } 0, @bar # notall | ||
| 110 | # Note that these implementations do not fully short-circuit | ||
| 111 | |||
| 112 | If your algorithm requires that C<reduce> produce an identity value, then make | ||
| 113 | sure that you always pass that identity value as the first argument to prevent | ||
| 114 | C<undef> being returned | ||
| 115 | |||
| 116 | $foo = reduce { $a + $b } 0, @values; # sum with 0 identity value | ||
| 117 | |||
| 118 | The above example code blocks also suggest how to use C<reduce> to build a | ||
| 119 | more efficient combined version of one of these basic functions and a C<map> | ||
| 120 | block. For example, to find the total length of all the strings in a list, | ||
| 121 | we could use | ||
| 122 | |||
| 123 | $total = sum map { length } @strings; | ||
| 124 | |||
| 125 | However, this produces a list of temporary integer values as long as the | ||
| 126 | original list of strings, only to reduce it down to a single value again. We | ||
| 127 | can compute the same result more efficiently by using C<reduce> with a code | ||
| 128 | block that accumulates lengths by writing this instead as: | ||
| 129 | |||
| 130 | $total = reduce { $a + length $b } 0, @strings | ||
| 131 | |||
| 132 | The remaining list-reduction functions are all specialisations of this generic | ||
| 133 | idea. | ||
| 134 | |||
| 135 | =head2 any | ||
| 136 | |||
| 137 | my $bool = any { BLOCK } @list; | ||
| 138 | |||
| 139 | I<Since version 1.33.> | ||
| 140 | |||
| 141 | Similar to C<grep> in that it evaluates C<BLOCK> setting C<$_> to each element | ||
| 142 | of C<@list> in turn. C<any> returns true if any element makes the C<BLOCK> | ||
| 143 | return a true value. If C<BLOCK> never returns true or C<@list> was empty then | ||
| 144 | it returns false. | ||
| 145 | |||
| 146 | Many cases of using C<grep> in a conditional can be written using C<any> | ||
| 147 | instead, as it can short-circuit after the first true result. | ||
| 148 | |||
| 149 | if( any { length > 10 } @strings ) { | ||
| 150 | # at least one string has more than 10 characters | ||
| 151 | } | ||
| 152 | |||
| 153 | Note: Due to XS issues the block passed may be able to access the outer @_ | ||
| 154 | directly. This is not intentional and will break under debugger. | ||
| 155 | |||
| 156 | =head2 all | ||
| 157 | |||
| 158 | my $bool = all { BLOCK } @list; | ||
| 159 | |||
| 160 | I<Since version 1.33.> | ||
| 161 | |||
| 162 | Similar to L</any>, except that it requires all elements of the C<@list> to | ||
| 163 | make the C<BLOCK> return true. If any element returns false, then it returns | ||
| 164 | false. If the C<BLOCK> never returns false or the C<@list> was empty then it | ||
| 165 | returns true. | ||
| 166 | |||
| 167 | Note: Due to XS issues the block passed may be able to access the outer @_ | ||
| 168 | directly. This is not intentional and will break under debugger. | ||
| 169 | |||
| 170 | =head2 none | ||
| 171 | |||
| 172 | =head2 notall | ||
| 173 | |||
| 174 | my $bool = none { BLOCK } @list; | ||
| 175 | |||
| 176 | my $bool = notall { BLOCK } @list; | ||
| 177 | |||
| 178 | I<Since version 1.33.> | ||
| 179 | |||
| 180 | Similar to L</any> and L</all>, but with the return sense inverted. C<none> | ||
| 181 | returns true only if no value in the C<@list> causes the C<BLOCK> to return | ||
| 182 | true, and C<notall> returns true only if not all of the values do. | ||
| 183 | |||
| 184 | Note: Due to XS issues the block passed may be able to access the outer @_ | ||
| 185 | directly. This is not intentional and will break under debugger. | ||
| 186 | |||
| 187 | =head2 first | ||
| 188 | |||
| 189 | my $val = first { BLOCK } @list; | ||
| 190 | |||
| 191 | Similar to C<grep> in that it evaluates C<BLOCK> setting C<$_> to each element | ||
| 192 | of C<@list> in turn. C<first> returns the first element where the result from | ||
| 193 | C<BLOCK> is a true value. If C<BLOCK> never returns true or C<@list> was empty | ||
| 194 | then C<undef> is returned. | ||
| 195 | |||
| 196 | $foo = first { defined($_) } @list # first defined value in @list | ||
| 197 | $foo = first { $_ > $value } @list # first value in @list which | ||
| 198 | # is greater than $value | ||
| 199 | |||
| 200 | =head2 max | ||
| 201 | |||
| 202 | my $num = max @list; | ||
| 203 | |||
| 204 | Returns the entry in the list with the highest numerical value. If the list is | ||
| 205 | empty then C<undef> is returned. | ||
| 206 | |||
| 207 | $foo = max 1..10 # 10 | ||
| 208 | $foo = max 3,9,12 # 12 | ||
| 209 | $foo = max @bar, @baz # whatever | ||
| 210 | |||
| 211 | =head2 maxstr | ||
| 212 | |||
| 213 | my $str = maxstr @list; | ||
| 214 | |||
| 215 | Similar to L</max>, but treats all the entries in the list as strings and | ||
| 216 | returns the highest string as defined by the C<gt> operator. If the list is | ||
| 217 | empty then C<undef> is returned. | ||
| 218 | |||
| 219 | $foo = maxstr 'A'..'Z' # 'Z' | ||
| 220 | $foo = maxstr "hello","world" # "world" | ||
| 221 | $foo = maxstr @bar, @baz # whatever | ||
| 222 | |||
| 223 | =head2 min | ||
| 224 | |||
| 225 | my $num = min @list; | ||
| 226 | |||
| 227 | Similar to L</max> but returns the entry in the list with the lowest numerical | ||
| 228 | value. If the list is empty then C<undef> is returned. | ||
| 229 | |||
| 230 | $foo = min 1..10 # 1 | ||
| 231 | $foo = min 3,9,12 # 3 | ||
| 232 | $foo = min @bar, @baz # whatever | ||
| 233 | |||
| 234 | =head2 minstr | ||
| 235 | |||
| 236 | my $str = minstr @list; | ||
| 237 | |||
| 238 | Similar to L</min>, but treats all the entries in the list as strings and | ||
| 239 | returns the lowest string as defined by the C<lt> operator. If the list is | ||
| 240 | empty then C<undef> is returned. | ||
| 241 | |||
| 242 | $foo = minstr 'A'..'Z' # 'A' | ||
| 243 | $foo = minstr "hello","world" # "hello" | ||
| 244 | $foo = minstr @bar, @baz # whatever | ||
| 245 | |||
| 246 | =head2 product | ||
| 247 | |||
| 248 | my $num = product @list; | ||
| 249 | |||
| 250 | I<Since version 1.35.> | ||
| 251 | |||
| 252 | Returns the numerical product of all the elements in C<@list>. If C<@list> is | ||
| 253 | empty then C<1> is returned. | ||
| 254 | |||
| 255 | $foo = product 1..10 # 3628800 | ||
| 256 | $foo = product 3,9,12 # 324 | ||
| 257 | |||
| 258 | =head2 sum | ||
| 259 | |||
| 260 | my $num_or_undef = sum @list; | ||
| 261 | |||
| 262 | Returns the numerical sum of all the elements in C<@list>. For backwards | ||
| 263 | compatibility, if C<@list> is empty then C<undef> is returned. | ||
| 264 | |||
| 265 | $foo = sum 1..10 # 55 | ||
| 266 | $foo = sum 3,9,12 # 24 | ||
| 267 | $foo = sum @bar, @baz # whatever | ||
| 268 | |||
| 269 | =head2 sum0 | ||
| 270 | |||
| 271 | my $num = sum0 @list; | ||
| 272 | |||
| 273 | I<Since version 1.26.> | ||
| 274 | |||
| 275 | Similar to L</sum>, except this returns 0 when given an empty list, rather | ||
| 276 | than C<undef>. | ||
| 277 | |||
| 278 | =cut | ||
| 279 | |||
| 280 | =head1 KEY/VALUE PAIR LIST FUNCTIONS | ||
| 281 | |||
| 282 | The following set of functions, all inspired by L<List::Pairwise>, consume an | ||
| 283 | even-sized list of pairs. The pairs may be key/value associations from a hash, | ||
| 284 | or just a list of values. The functions will all preserve the original ordering | ||
| 285 | of the pairs, and will not be confused by multiple pairs having the same "key" | ||
| 286 | value - nor even do they require that the first of each pair be a plain string. | ||
| 287 | |||
| 288 | B<NOTE>: At the time of writing, the following C<pair*> functions that take a | ||
| 289 | block do not modify the value of C<$_> within the block, and instead operate | ||
| 290 | using the C<$a> and C<$b> globals instead. This has turned out to be a poor | ||
| 291 | design, as it precludes the ability to provide a C<pairsort> function. Better | ||
| 292 | would be to pass pair-like objects as 2-element array references in C<$_>, in | ||
| 293 | a style similar to the return value of the C<pairs> function. At some future | ||
| 294 | version this behaviour may be added. | ||
| 295 | |||
| 296 | Until then, users are alerted B<NOT> to rely on the value of C<$_> remaining | ||
| 297 | unmodified between the outside and the inside of the control block. In | ||
| 298 | particular, the following example is B<UNSAFE>: | ||
| 299 | |||
| 300 | my @kvlist = ... | ||
| 301 | |||
| 302 | foreach (qw( some keys here )) { | ||
| 303 | my @items = pairgrep { $a eq $_ } @kvlist; | ||
| 304 | ... | ||
| 305 | } | ||
| 306 | |||
| 307 | Instead, write this using a lexical variable: | ||
| 308 | |||
| 309 | foreach my $key (qw( some keys here )) { | ||
| 310 | my @items = pairgrep { $a eq $key } @kvlist; | ||
| 311 | ... | ||
| 312 | } | ||
| 313 | |||
| 314 | =cut | ||
| 315 | |||
| 316 | =head2 pairs | ||
| 317 | |||
| 318 | my @pairs = pairs @kvlist; | ||
| 319 | |||
| 320 | I<Since version 1.29.> | ||
| 321 | |||
| 322 | A convenient shortcut to operating on even-sized lists of pairs, this function | ||
| 323 | returns a list of C<ARRAY> references, each containing two items from the | ||
| 324 | given list. It is a more efficient version of | ||
| 325 | |||
| 326 | @pairs = pairmap { [ $a, $b ] } @kvlist | ||
| 327 | |||
| 328 | It is most convenient to use in a C<foreach> loop, for example: | ||
| 329 | |||
| 330 | foreach my $pair ( pairs @kvlist ) { | ||
| 331 | my ( $key, $value ) = @$pair; | ||
| 332 | ... | ||
| 333 | } | ||
| 334 | |||
| 335 | Since version C<1.39> these C<ARRAY> references are blessed objects, | ||
| 336 | recognising the two methods C<key> and C<value>. The following code is | ||
| 337 | equivalent: | ||
| 338 | |||
| 339 | foreach my $pair ( pairs @kvlist ) { | ||
| 340 | my $key = $pair->key; | ||
| 341 | my $value = $pair->value; | ||
| 342 | ... | ||
| 343 | } | ||
| 344 | |||
| 345 | =head2 unpairs | ||
| 346 | |||
| 347 | my @kvlist = unpairs @pairs | ||
| 348 | |||
| 349 | I<Since version 1.42.> | ||
| 350 | |||
| 351 | The inverse function to C<pairs>; this function takes a list of C<ARRAY> | ||
| 352 | references containing two elements each, and returns a flattened list of the | ||
| 353 | two values from each of the pairs, in order. This is notionally equivalent to | ||
| 354 | |||
| 355 | my @kvlist = map { @{$_}[0,1] } @pairs | ||
| 356 | |||
| 357 | except that it is implemented more efficiently internally. Specifically, for | ||
| 358 | any input item it will extract exactly two values for the output list; using | ||
| 359 | C<undef> if the input array references are short. | ||
| 360 | |||
| 361 | Between C<pairs> and C<unpairs>, a higher-order list function can be used to | ||
| 362 | operate on the pairs as single scalars; such as the following near-equivalents | ||
| 363 | of the other C<pair*> higher-order functions: | ||
| 364 | |||
| 365 | @kvlist = unpairs grep { FUNC } pairs @kvlist | ||
| 366 | # Like pairgrep, but takes $_ instead of $a and $b | ||
| 367 | |||
| 368 | @kvlist = unpairs map { FUNC } pairs @kvlist | ||
| 369 | # Like pairmap, but takes $_ instead of $a and $b | ||
| 370 | |||
| 371 | Note however that these versions will not behave as nicely in scalar context. | ||
| 372 | |||
| 373 | Finally, this technique can be used to implement a sort on a keyvalue pair | ||
| 374 | list; e.g.: | ||
| 375 | |||
| 376 | @kvlist = unpairs sort { $a->key cmp $b->key } pairs @kvlist | ||
| 377 | |||
| 378 | =head2 pairkeys | ||
| 379 | |||
| 380 | my @keys = pairkeys @kvlist; | ||
| 381 | |||
| 382 | I<Since version 1.29.> | ||
| 383 | |||
| 384 | A convenient shortcut to operating on even-sized lists of pairs, this function | ||
| 385 | returns a list of the the first values of each of the pairs in the given list. | ||
| 386 | It is a more efficient version of | ||
| 387 | |||
| 388 | @keys = pairmap { $a } @kvlist | ||
| 389 | |||
| 390 | =head2 pairvalues | ||
| 391 | |||
| 392 | my @values = pairvalues @kvlist; | ||
| 393 | |||
| 394 | I<Since version 1.29.> | ||
| 395 | |||
| 396 | A convenient shortcut to operating on even-sized lists of pairs, this function | ||
| 397 | returns a list of the the second values of each of the pairs in the given list. | ||
| 398 | It is a more efficient version of | ||
| 399 | |||
| 400 | @values = pairmap { $b } @kvlist | ||
| 401 | |||
| 402 | =head2 pairgrep | ||
| 403 | |||
| 404 | my @kvlist = pairgrep { BLOCK } @kvlist; | ||
| 405 | |||
| 406 | my $count = pairgrep { BLOCK } @kvlist; | ||
| 407 | |||
| 408 | I<Since version 1.29.> | ||
| 409 | |||
| 410 | Similar to perl's C<grep> keyword, but interprets the given list as an | ||
| 411 | even-sized list of pairs. It invokes the C<BLOCK> multiple times, in scalar | ||
| 412 | context, with C<$a> and C<$b> set to successive pairs of values from the | ||
| 413 | C<@kvlist>. | ||
| 414 | |||
| 415 | Returns an even-sized list of those pairs for which the C<BLOCK> returned true | ||
| 416 | in list context, or the count of the B<number of pairs> in scalar context. | ||
| 417 | (Note, therefore, in scalar context that it returns a number half the size of | ||
| 418 | the count of items it would have returned in list context). | ||
| 419 | |||
| 420 | @subset = pairgrep { $a =~ m/^[[:upper:]]+$/ } @kvlist | ||
| 421 | |||
| 422 | As with C<grep> aliasing C<$_> to list elements, C<pairgrep> aliases C<$a> and | ||
| 423 | C<$b> to elements of the given list. Any modifications of it by the code block | ||
| 424 | will be visible to the caller. | ||
| 425 | |||
| 426 | =head2 pairfirst | ||
| 427 | |||
| 428 | my ( $key, $val ) = pairfirst { BLOCK } @kvlist; | ||
| 429 | |||
| 430 | my $found = pairfirst { BLOCK } @kvlist; | ||
| 431 | |||
| 432 | I<Since version 1.30.> | ||
| 433 | |||
| 434 | Similar to the L</first> function, but interprets the given list as an | ||
| 435 | even-sized list of pairs. It invokes the C<BLOCK> multiple times, in scalar | ||
| 436 | context, with C<$a> and C<$b> set to successive pairs of values from the | ||
| 437 | C<@kvlist>. | ||
| 438 | |||
| 439 | Returns the first pair of values from the list for which the C<BLOCK> returned | ||
| 440 | true in list context, or an empty list of no such pair was found. In scalar | ||
| 441 | context it returns a simple boolean value, rather than either the key or the | ||
| 442 | value found. | ||
| 443 | |||
| 444 | ( $key, $value ) = pairfirst { $a =~ m/^[[:upper:]]+$/ } @kvlist | ||
| 445 | |||
| 446 | As with C<grep> aliasing C<$_> to list elements, C<pairfirst> aliases C<$a> and | ||
| 447 | C<$b> to elements of the given list. Any modifications of it by the code block | ||
| 448 | will be visible to the caller. | ||
| 449 | |||
| 450 | =head2 pairmap | ||
| 451 | |||
| 452 | my @list = pairmap { BLOCK } @kvlist; | ||
| 453 | |||
| 454 | my $count = pairmap { BLOCK } @kvlist; | ||
| 455 | |||
| 456 | I<Since version 1.29.> | ||
| 457 | |||
| 458 | Similar to perl's C<map> keyword, but interprets the given list as an | ||
| 459 | even-sized list of pairs. It invokes the C<BLOCK> multiple times, in list | ||
| 460 | context, with C<$a> and C<$b> set to successive pairs of values from the | ||
| 461 | C<@kvlist>. | ||
| 462 | |||
| 463 | Returns the concatenation of all the values returned by the C<BLOCK> in list | ||
| 464 | context, or the count of the number of items that would have been returned in | ||
| 465 | scalar context. | ||
| 466 | |||
| 467 | @result = pairmap { "The key $a has value $b" } @kvlist | ||
| 468 | |||
| 469 | As with C<map> aliasing C<$_> to list elements, C<pairmap> aliases C<$a> and | ||
| 470 | C<$b> to elements of the given list. Any modifications of it by the code block | ||
| 471 | will be visible to the caller. | ||
| 472 | |||
| 473 | See L</KNOWN BUGS> for a known-bug with C<pairmap>, and a workaround. | ||
| 474 | |||
| 475 | =cut | ||
| 476 | |||
| 477 | =head1 OTHER FUNCTIONS | ||
| 478 | |||
| 479 | =cut | ||
| 480 | |||
| 481 | =head2 shuffle | ||
| 482 | |||
| 483 | my @values = shuffle @values; | ||
| 484 | |||
| 485 | Returns the values of the input in a random order | ||
| 486 | |||
| 487 | @cards = shuffle 0..51 # 0..51 in a random order | ||
| 488 | |||
| 489 | =head2 uniq | ||
| 490 | |||
| 491 | my @subset = uniq @values | ||
| 492 | |||
| 493 | I<Since version 1.45.> | ||
| 494 | |||
| 495 | Filters a list of values to remove subsequent duplicates, as judged by a | ||
| 496 | DWIM-ish string equality or C<undef> test. Preserves the order of unique | ||
| 497 | elements, and retains the first value of any duplicate set. | ||
| 498 | |||
| 499 | my $count = uniq @values | ||
| 500 | |||
| 501 | In scalar context, returns the number of elements that would have been | ||
| 502 | returned as a list. | ||
| 503 | |||
| 504 | The C<undef> value is treated by this function as distinct from the empty | ||
| 505 | string, and no warning will be produced. It is left as-is in the returned | ||
| 506 | list. Subsequent C<undef> values are still considered identical to the first, | ||
| 507 | and will be removed. | ||
| 508 | |||
| 509 | =head2 uniqnum | ||
| 510 | |||
| 511 | my @subset = uniqnum @values | ||
| 512 | |||
| 513 | I<Since version 1.44.> | ||
| 514 | |||
| 515 | Filters a list of values to remove subsequent duplicates, as judged by a | ||
| 516 | numerical equality test. Preserves the order of unique elements, and retains | ||
| 517 | the first value of any duplicate set. | ||
| 518 | |||
| 519 | my $count = uniqnum @values | ||
| 520 | |||
| 521 | In scalar context, returns the number of elements that would have been | ||
| 522 | returned as a list. | ||
| 523 | |||
| 524 | Note that C<undef> is treated much as other numerical operations treat it; it | ||
| 525 | compares equal to zero but additionally produces a warning if such warnings | ||
| 526 | are enabled (C<use warnings 'uninitialized';>). In addition, an C<undef> in | ||
| 527 | the returned list is coerced into a numerical zero, so that the entire list of | ||
| 528 | values returned by C<uniqnum> are well-behaved as numbers. | ||
| 529 | |||
| 530 | Note also that multiple IEEE C<NaN> values are treated as duplicates of | ||
| 531 | each other, regardless of any differences in their payloads, and despite | ||
| 532 | the fact that C<< 0+'NaN' == 0+'NaN' >> yields false. | ||
| 533 | |||
| 534 | =head2 uniqstr | ||
| 535 | |||
| 536 | my @subset = uniqstr @values | ||
| 537 | |||
| 538 | I<Since version 1.45.> | ||
| 539 | |||
| 540 | Filters a list of values to remove subsequent duplicates, as judged by a | ||
| 541 | string equality test. Preserves the order of unique elements, and retains the | ||
| 542 | first value of any duplicate set. | ||
| 543 | |||
| 544 | my $count = uniqstr @values | ||
| 545 | |||
| 546 | In scalar context, returns the number of elements that would have been | ||
| 547 | returned as a list. | ||
| 548 | |||
| 549 | Note that C<undef> is treated much as other string operations treat it; it | ||
| 550 | compares equal to the empty string but additionally produces a warning if such | ||
| 551 | warnings are enabled (C<use warnings 'uninitialized';>). In addition, an | ||
| 552 | C<undef> in the returned list is coerced into an empty string, so that the | ||
| 553 | entire list of values returned by C<uniqstr> are well-behaved as strings. | ||
| 554 | |||
| 555 | =cut | ||
| 556 | |||
| 557 | =head2 head | ||
| 558 | |||
| 559 | my @values = head $size, @list; | ||
| 560 | |||
| 561 | Returns the first C<$size> elements from C<@list>. If C<$size> is negative, returns | ||
| 562 | all but the last C<$size> elements from C<@list>. | ||
| 563 | |||
| 564 | @result = head 2, qw( foo bar baz ); | ||
| 565 | # foo, bar | ||
| 566 | |||
| 567 | @result = head -2, qw( foo bar baz ); | ||
| 568 | # foo | ||
| 569 | |||
| 570 | =head2 tail | ||
| 571 | |||
| 572 | my @values = tail $size, @list; | ||
| 573 | |||
| 574 | Returns the last C<$size> elements from C<@list>. If C<$size> is negative, returns | ||
| 575 | all but the first C<$size> elements from C<@list>. | ||
| 576 | |||
| 577 | @result = tail 2, qw( foo bar baz ); | ||
| 578 | # bar, baz | ||
| 579 | |||
| 580 | @result = tail -2, qw( foo bar baz ); | ||
| 581 | # baz | ||
| 582 | |||
| 583 | =head1 KNOWN BUGS | ||
| 584 | |||
| 585 | =head2 RT #95409 | ||
| 586 | |||
| 587 | L<https://rt.cpan.org/Ticket/Display.html?id=95409> | ||
| 588 | |||
| 589 | If the block of code given to L</pairmap> contains lexical variables that are | ||
| 590 | captured by a returned closure, and the closure is executed after the block | ||
| 591 | has been re-used for the next iteration, these lexicals will not see the | ||
| 592 | correct values. For example: | ||
| 593 | |||
| 594 | my @subs = pairmap { | ||
| 595 | my $var = "$a is $b"; | ||
| 596 | sub { print "$var\n" }; | ||
| 597 | } one => 1, two => 2, three => 3; | ||
| 598 | |||
| 599 | $_->() for @subs; | ||
| 600 | |||
| 601 | Will incorrectly print | ||
| 602 | |||
| 603 | three is 3 | ||
| 604 | three is 3 | ||
| 605 | three is 3 | ||
| 606 | |||
| 607 | This is due to the performance optimisation of using C<MULTICALL> for the code | ||
| 608 | block, which means that fresh SVs do not get allocated for each call to the | ||
| 609 | block. Instead, the same SV is re-assigned for each iteration, and all the | ||
| 610 | closures will share the value seen on the final iteration. | ||
| 611 | |||
| 612 | To work around this bug, surround the code with a second set of braces. This | ||
| 613 | creates an inner block that defeats the C<MULTICALL> logic, and does get fresh | ||
| 614 | SVs allocated each time: | ||
| 615 | |||
| 616 | my @subs = pairmap { | ||
| 617 | { | ||
| 618 | my $var = "$a is $b"; | ||
| 619 | sub { print "$var\n"; } | ||
| 620 | } | ||
| 621 | } one => 1, two => 2, three => 3; | ||
| 622 | |||
| 623 | This bug only affects closures that are generated by the block but used | ||
| 624 | afterwards. Lexical variables that are only used during the lifetime of the | ||
| 625 | block's execution will take their individual values for each invocation, as | ||
| 626 | normal. | ||
| 627 | |||
| 628 | =head2 uniqnum() on oversized bignums | ||
| 629 | |||
| 630 | Due to the way that C<uniqnum()> compares numbers, it cannot distinguish | ||
| 631 | differences between bignums (especially bigints) that are too large to fit in | ||
| 632 | the native platform types. For example, | ||
| 633 | |||
| 634 | my $x = Math::BigInt->new( "1" x 100 ); | ||
| 635 | my $y = $x + 1; | ||
| 636 | |||
| 637 | say for uniqnum( $x, $y ); | ||
| 638 | |||
| 639 | Will print just the value of C<$x>, believing that C<$y> is a numerically- | ||
| 640 | equivalent value. This bug does not affect C<uniqstr()>, which will correctly | ||
| 641 | observe that the two values stringify to different strings. | ||
| 642 | |||
| 643 | =head1 SUGGESTED ADDITIONS | ||
| 644 | |||
| 645 | The following are additions that have been requested, but I have been reluctant | ||
| 646 | to add due to them being very simple to implement in perl | ||
| 647 | |||
| 648 | # How many elements are true | ||
| 649 | |||
| 650 | sub true { scalar grep { $_ } @_ } | ||
| 651 | |||
| 652 | # How many elements are false | ||
| 653 | |||
| 654 | sub false { scalar grep { !$_ } @_ } | ||
| 655 | |||
| 656 | =head1 SEE ALSO | ||
| 657 | |||
| 658 | L<Scalar::Util>, L<List::MoreUtils> | ||
| 659 | |||
| 660 | =head1 COPYRIGHT | ||
| 661 | |||
| 662 | Copyright (c) 1997-2007 Graham Barr <gbarr@pobox.com>. All rights reserved. | ||
| 663 | This program is free software; you can redistribute it and/or | ||
| 664 | modify it under the same terms as Perl itself. | ||
| 665 | |||
| 666 | Recent additions and current maintenance by | ||
| 667 | Paul Evans, <leonerd@leonerd.org.uk>. | ||
| 668 | |||
| 669 | =cut | ||
| 670 | |||
| 671 | 1; |