Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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. ...

Cheers Rolf


In reply to Re: Name "main::a" used only once: possible typo (limitations of "sort" like functions) by LanX
in thread Name "main::a" used only once: possible typo by szabgab

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (2)
As of 2024-04-19 22:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found