On 9/12/07, Paul Lussier <[EMAIL PROTECTED]> wrote:
>>   You don't need to put parenthesis around arguments to split, and you
>> don't need to explicitly specify the default pattern match target
>> ($_).
>
> Unfortunately, you both "don't *need* to" and "*can* do" anything in
> perl.  Often at the same time!  This is what leads to very difficult
> to read, and more difficult to maintain perl code.

  Well, this is something of a religious issue.  :)  But here's my opinion:

  Personally, in the proper context, I find this:

        @foo = split m{blah};

to be easier to read and comprehend at a glance than this:

        @foo = split (m{blah}, $_);

  The "proper context" being a loop or other pattern where I'm running
a series of operations on a series of inputs (i.e., lines).  In those
contexts, where the whole point is to perform a series of operations
on each input, assembly-line fashion, using the the implicit argument
lets me focus on what the code is actually *doing*.  Explicitly
specifying $_ over and over again just clutters up the code with
pointless syntax.  It's one more thing my brain has to recognize and
process.

  I don't arbitrarily assign to $_ and use it at random, the way some
people do.  And I do make use of parenthesis, braces, and such, even
when they are not needed, when I find it makes the code clearer.  But
I also leave them out when I find it makes the code clearer.

  For a slightly less contrived example, take a script which trims
leading and trailing whitespace from each line in an input file.  I
already have one implementation, and I just wrote up another one.
Here's one of the possible implementations:

#!/usr/bin/perl
$filename = $ARGV[0];
if ($filename eq "") {
    open(INPUTFILE, "<&STDIN");
}
else {
    open(INPUTFILE, "< $filename") or die("could not open input file!");
}
while(not(eof(INPUTFILE))) {
    $line = <INPUTFILE>;
    $line =~ s/^[\x20\t]*//; # trim leading space
    $line =~ s/[\x20\t]*$//; # trim trailing space
    print($line);
}

  And here is another possibility:

#!/usr/bin/perl -p
s/^[\x20\t]*//; # trim leading space
s/[\x20\t]*$//; # trim trailing space

  Assuming the reader is familiar with the language, which do you
think will be easier/quicker to comprehend?

  It may be true that someone who *isn't* familiar with Perl would
find it easier to puzzle out the meaning of the longer version.  But I
don't find that a particularly compelling argument.  I write Perl
programs with the assumption that the reader understands Perl, the
same way I am assuming readers of this message understand English.  :)

  This may mean Perl, as practiced, is harder to learn than a language
which is more rigid and always verbose.  Many say similar things about
Unix.  Or Emacs.  :-)  I'm don't argue that one approach is right and
the other wrong, but I do think that both approaches have their
merits.

  YMMV.

-- Ben
_______________________________________________
gnhlug-discuss mailing list
gnhlug-discuss@mail.gnhlug.org
http://mail.gnhlug.org/mailman/listinfo/gnhlug-discuss/

Reply via email to