[sf-perl] use {strict,warnings}

Joe Brenner doom at kzsu.stanford.edu
Tue Mar 4 16:05:20 PST 2008


Kevin Frost <biztos at mac.com> wrote:

> That's a good example.  I would say you can rewrite it so it throws no
> warnings and is still reasonably elegant, but I would use the first
> version (no warnings) before using the second (gratuitous ||=).
>
> The warnings are telling you that your assumption there are three
> parts to each phrase is dubious.  That's a useful warning, though it's
> arguably a nit-picking sort of usefulness.
>
> You could do something like this:
>
>      # pretty ugly, but it works and the print didn't change:
>      my ( $alpha, $ralpha, $blvd ) =
>        map { defined $_ ? $_ : '' } split( /\s+/, $phrase, 3 ), '',
> '', '';
>      print " 1: $alpha\n 2: $ralpha\n 3: $blvd\n\n";
>
> ...or like this:
>
>      my $i = 0;
>      print join( "\n", map { $i++; "$i: $_"; } split( /\s+/,
> $phrase ) ), "\n\n";
>
> ...or like this if you don't care about 4+:
>
>      my $i = 0;
>      print join( "\n", map { $i++; "$i: $_"; } split( /\s+/, $phrase,
> 3 ) ),
>        "\n\n";
>
> ...but if you don't like those approaches then I think the no-warnings
> bit is in exactly the right spot.

It isn't so much that I don't like them, it's that working out
something like one of those solutions would've taken me a few
minutes, and worse, it probably would take someone reading the
code a few minutes to understand.

(I did have the thought if I was splitting to an array, with an
indefinite number of items, it would be time to use map.)

Like I was saying before:

> >> In this particular case it's something of a wash in terms of
> >> complexity or dangerousness, but you don't have to think as
> >> hard about the second way [leaving warnings on and
> >> manually applying a default], so I think that one wins.

Conserving mental effort is the goal, not reducing the number
of lines of code.



More information about the SanFrancisco-pm mailing list