I gather your ... represents other arguments not germaine to your question.
Why not do $color ||= 0? It would help show the numerical default for an unspecified color.
You allow dash (-) in your filter, but then clamp to zero (I suppose to make sure -4 goes to 0, not 4). You don't clamp to 255, so a color could be 400. Do you want to allow input decimal numbers like 40.3?
You split up to 4 elements but only want three.
The method of backfilling your array (via a slice of split(wanted, padding)) is workable but obtuse.
my $color = shift; my @colors = split(/,/, $color, 3); for (0 .. 2) { $colors[$_] = int($colors[$_] || 0); $colors[$_] = 0 if $colors[$_] < 0; $colors[$_] = 255 if $colors[$_] > 255; $colors[$_] /= 255; }
Now to review my own version. If I don't want lvalue behaviors from @_, I would probably re-use @_ for the work instead of the above @colors. I used the odd for (0 .. 2) indexing method instead of trying to backfill and slice. I purposely didn't play golf here.
In my opinion, there's no such thing as "too DWIM-ish" as long as the results are what a reasonable person would expect. "Be lenient in what you accept, be strict in what you produce." When being DWIM, though, it's even more important to have readable code so that the user can understand the magic when it's not DWTM.
--
[ e d @ h a l l e y . c c ]
In reply to Re: DWIM on sub input
by halley
in thread DWIM on sub input
by dragonchild
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |