Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Name "main::a" used only once: possible typo (limitations of "sort" like functions)

by LanX (Saint)
on Mar 05, 2013 at 21:57 UTC ( [id://1021906]=note: print w/replies, xml ) Need Help??


in reply to Name "main::a" used only once: possible typo

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

  • Comment on Re: Name "main::a" used only once: possible typo (limitations of "sort" like functions)
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: Name "main::a" used only once: possible typo (limitations of "sort" like functions)
by szabgab (Priest) on Mar 06, 2013 at 05:13 UTC
    LanX, what I don't understand is why this warning, and the way to deal with it by the user of the module is not mentioned in the documentation as a limitation of perl/the module.

    If the situation I described is real, then I assume many people bump into it and many people think they are doing something wrong.

    So unless the code I wrote has the flaw, it would IMHO better to warn the user about the warning.

      > LanX, what I don't understand is why this warning, and the way to deal with it by the user of the module is not mentioned in the documentation as a limitation of perl/the module.

      I can only suppose that the problems are so overwhelming, that the authors don't know how to describe them.

      Cheers Rolf

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1021906]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-09-20 19:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (26 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.