Hi Gabor
> Why did the first example generate the warnings?
Wrong question. Since sort is a built-in, it was able to silence the warnings!
> What is the recommended way to silence them?
Use them twice?
> Why is this not mentioned in List::Util?
Maybe because the author is frustrated about not beeing able to simulate a built-in?
Seriously ...
... it's not that easy to design functional constructs with special variables in code-blocks like sort does.
I tried it in hgrep and then I was confronted with many scoping problems, what if the block is defined within another package or if $a and $b are declared as lexicals?
Look into the code of Hash::MostUtils to have an idea about the complications to solve all of this.
I bet that List::Util couldn't solve all of this.
UPDATE
To prove my last guess:
This prints 55, but after uncommenting the second line its only 0 ...oops!
use List::Util qw/reduce/;
# my ($a,$b)=(0,0);
print reduce { $b+=$a } 1..10;
UPDATE
and here the same problem for List::MoreUtils
use warnings;
use strict;
# my ($a,$b)=(0,0);
use List::MoreUtils qw/pairwise/;
my @a = (1 .. 5);
my @b = (11 .. 15);
my @x = pairwise { $a + $b } @a, @b; # returns 12, 14, 16, 18, 20
use Data::Dump;
dd \@x;
UPDATE
At least sort simply dies
my ($a,$b)=(0,0);
print sort { $b cmp $a } 1..10;
Can't use "my $b" in sort comparison at ... line 2.
Update
...but
use warnings;
use strict;
our ($a,$b);
package Tst;
print sort { $b cmp $a } 1..10;
Use of uninitialized value $b in string comparison (cmp) at /home/lanx
+/B/PL/PM/ScopeListUtils.pl line 9.
Use of uninitialized value $a in string comparison (cmp) at /home/lanx
+/B/PL/PM/ScopeListUtils.pl line 9.
Use of uninitialized value $b in string comparison (cmp) at /home/lanx
+/B/PL/PM/ScopeListUtils.pl line 9.
Use of uninitialized value $a in string comparison (cmp) at /home/lanx
+/B/PL/PM/ScopeListUtils.pl line 9.
...
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.