http://www.perlmonks.org?node_id=248807


in reply to Re: Surpised by foreach iterator limitation
in thread Surpised by foreach iterator limitation

I thought the last paragraph of my post made it clear but perhaps not. I had an involved sub (actually subroutine generator) for validating data. It takes as it's argument a hash. The sub references this in may places. I wanted to refactor it to take multiple arguments for each call. Currently it expects one piece of data. I figured the easiest way and least likely way to intruduce bugs would be to wrap the code in a loop. For example
sub example { # Expects argument data that is a single value my %p = (@_); # return if $p{required} && !$p{data}; Do stuff that references $p{data}... return 1; # OK } my $value = 10; if ( example(data=>$value, minval=>5, maxval=>10){ print "$value is OK!\n"; }else{ die "$value is naughty!"; } Now let's say I want to extended it to take multiple values I thought the easiest way would be to do this. sub example { my %p = (@_); # my @data = UNIVERSAL::isa($p{data},'ARRAY') ? @{$p{data}} : ($p{ +data}); foreach $p{data} (@data){ return if $p{required} && !$p{data}; Do stuff that references $p{data}... } return 1; # OK }
It did not work.

This can be solved in a million simple ways such as using a lexical for the iterator and simply$hash{key} = $iterator_variable as the first statement in the loop.) and I am not looking for a fix. I was simply suprised the Perl could not parse this.

-Lee

"To be civilized is to deny one's nature."