Front page | perl.perl5.porters |
Postings from January 2012
Re: [perl #85150] each on an anonymous array constructor doesn'tcomplain, but sorta works
Thread Previous
|
Thread Next
From:
Brad Gilbert
Date:
January 3, 2012 17:02
Subject:
Re: [perl #85150] each on an anonymous array constructor doesn'tcomplain, but sorta works
Message ID:
CAD2L-T1cLs_gHgya1a3+3CccfW2fsq-RVTyP6JovS85aGyX8Ww@mail.gmail.com
This should warn, or the anonymous array/hash reference should be the
same on every iteration.
while( my($k,$v) = each [0..3] ){ ... }
while( my($k,$v) = each {0..3} ){ ... }
If we make the reference the same on every iteration, it should
possibly warn if the user doesn't declare a minimum version.
---
These should at least warn, since there is no way to break out of the
loop without `goto`, or `last`.
There is also no possible change in each iteration.
( These are anonymous array, or hash references created with constant lists )
while( my($k,$v) = each [0..3] ){ ... }
while( my($k,$v) = each @{[0..3]} ){ ... }
while( my($k,$v) = each {0..3} ){ ... }
while( my($k,$v) = each %{{0..3}} ){ ... }
while( my($k,$v) = pop [0..3] ){ ... }
while( my($k,$v) = pop @{[0..3]} ){ ... }
while( my($k,$v) = shift [0..3] ){ ... }
while( my($k,$v) = shift @{[0..3]} ){ ... }
while( my $n = unshift [0..3], 0 ){ ... }
while( my $n = unshift @{[0..3]}, 0 ){ ... }
(possibly other builtins should be added to this list.)
---
These might be worth warning as well since it suffers the same problem.
I can see this being used by a Master Perl Guru to do something amazing though.
while( my($k,$v) = each my $arr = [0..3] ){ ... }
while( my($k,$v) = each @{ my $arr = [0..3] } ){ ... }
while( my($k,$v) = each my $hash = {0..3} ){ ... }
while( my($k,$v) = each %{ my $hash = {0..3} } ){ ... }
while( my($k,$v) = pop my $arr = [0..3] ){ ... }
while( my($k,$v) = pop @{ my $arr = [0..3] } ){ ... }
while( my($k,$v) = shift my $arr = [0..3] ){ ... }
while( my($k,$v) = shift @{ my $arr = [0..3] } ){ ... }
while( my $n = unshift my $arr = [0..3], 0 ){ ... }
while( my $n = unshift @{ my $arr = [0..3] }, 0 ){ ... }
---
These possibly shouldn't warn, or be modified since the value will
likely be different on each run.
while( my($k,$v) = each [rand] ){ ... }
while( my($k,$v) = each @{[rand]} ){ ... }
while( my($k,$v) = each [subroutine()] ){ ... }
while( my($k,$v) = each @{[subroutine()]} ){ ... }
etc.
---
These possibly shouldn't warn, or be modified since the value can
change in the loop, and thus be different on subsequent runs.
while( my($k,$v) = each [@ARGV] ){ ... }
while( my($k,$v) = each [@array] ){ ... }
while( my($k,$v) = each [%hash] ){ ... }
while( my($k,$v) = each [$_] ){ ... }
while( my($k,$v) = each [$scalar] ){ ... }
etc.
---
The warning should mention that it is because of interactions of a
while loop and an anonymous array/hash reference with a constant list
Thread Previous
|
Thread Next