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."
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.