in reply to DWIM on sub input

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 ]

Replies are listed 'Best First'.
Re: Re: DWIM on sub input
by dragonchild (Archbishop) on Jul 16, 2003 at 14:31 UTC
    A few explanations:
    • I split to 4 elements because of the case "1,2,3,4,5,6,7". I want to discard the extra elements.
    • Good catch on the upper bound. Thanks!
    • Yes, I do want to allow 40.3 and 0.01 and the like.
    • I don't want to re-use @_ because of the ... (which are the extra parameters that aren't germane to the discussion). In part, I'm still working the API so I may allow for craziness within @_.
    • The reason for the substitution instead of int is that int throws a warning. Plus, int('a1') != int('1a'), whereas my code will treat them the as the same.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      Split to 3 elements, because you only want to keep 3. You would have caught (1, 2, 3, 4) from "1,2,3,4,5,6,7".

      D'oh! I was wrong. But "4,5,6,7" == 4 ;)

      --
      [ e d @ h a l l e y . c c ]

        #!/usr/bin/perl my $x = '1,2,3,4,5,6,7'; my @x = split (/,/, $x, 3); my @y = (split (/,/, $x, 4))[0 .. 2]; { local $"="', '"; print "'@x'\n"; print "'@y'\n"; } ---------- '1', '2', '3,4,5,6,7' '1', '2', '3'
        I want to keep 3 good elements. Split to 4 and keep the first 3.

        ------
        We are the carpenters and bricklayers of the Information Age.

        Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

        Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.