The code is a little hard to read, for something so simple as a set of argument constraints. There's no dishonor in using more statements or more linebreaks to clarify the code.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.