[sf-perl] use {strict,warnings}

Kevin Frost biztos at mac.com
Tue Mar 4 09:18:31 PST 2008


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.

cheers

-- f.

On Mar 3, 2008, at 8:13 PM, Joe Brenner wrote:

> Joe Brenner <doom at kzsu.stanford.edu> wrote:
>
>> I think the question for me might be boiled down to this one code
>> example.  Is it better to code something like this:
>>
>>  use warnings;
>>  use strict;
>>
>>  my @phrases = (
>>              'The Rediscovery of Man',
>>              'The Instrumentality',
>>              'D\'Joan',
>>  );
>>
>>  foreach my $phrase (@phrases) {
>>    my ($alpha, $ralpha, $blvd) = split ' ', $phrase;
>>    {
>>      no warnings 'uninitialized';
>>      print " 1: $alpha\n 2: $ralpha\n 3: $blvd\n\n";
>>    }
>>  }
>>
>> Or should that last loop be handled something like this:
>>
>>  foreach my $phrase (@phrases) {
>>    my ($alpha, $ralpha, $blvd) = (' ', ' ', ' ');
>>    ($alpha, $ralpha, $blvd) = split ' ', $phrase;
>>    $alpha  ||='';
>>    $ralpha ||='';
>>    $blvd   ||='';
>>    print " 1: $alpha\n 2: $ralpha\n 3: $blvd\n\n";
>>  }
>>
>> 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, so I think that one wins.
>>
>> I would guess that just leaving warnings on all the time wins
>> often enough that you're better off doing it that way by
>> default, and living with the occasional minor pain.
>>
>
> Sorry, that second example was supposed to be like this:
>
>  foreach my $phrase (@phrases) {
>    my ($alpha, $ralpha, $blvd) = split ' ', $phrase;
>    $alpha  ||='';
>    $ralpha ||='';
>    $blvd   ||='';
>    print " 1: $alpha\n 2: $ralpha\n 3: $blvd\n\n";
>  }
>
>
> (I'd been experimenting with split for a minute there, making
> sure it behaved the way I thought it did...  You can not
> initialize the later variables first, and expect them to stay
> untouched by split if there aren't enough items returned: it
> always defines the later items as undef.)
>
> _______________________________________________
> SanFrancisco-pm mailing list
> SanFrancisco-pm at pm.org
> http://mail.pm.org/mailman/listinfo/sanfrancisco-pm



More information about the SanFrancisco-pm mailing list