[sf-perl] use {strict,warnings}

Joe Brenner doom at kzsu.stanford.edu
Mon Mar 3 20:13:32 PST 2008


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.)



More information about the SanFrancisco-pm mailing list