Re: Curly braces and the logic of PERL



From: Rob Dixon <rob.dixon@xxxxxxx>
I have a particular issue with statements like

/\Q$string/ && print $REPORT "$File::Find::name\n" and return;

which are ugly in the extreme and do not begin to read as English. The style
comes from Perl's origins on the Unix platform, and use of shortcuts like this,
that are familiar to Unix shell programmers, do nothing to help Perl's migration
to other platforms. The grep() function is a similar culprit - thank God there
is no sed() or awk().

I'd probably write that as

print $REPORT "$File::Find::name\n" and return if /\Q$string/;

or

if (/\Q$string/) {
print $REPORT "$File::Find::name\n";
return;
}

The first reads as English to me just fine.


OTOH, grep{} is a totally different thing. And

my @even = grep {$_ % 2 == 0} @numbers;

is definitely easier to validate than

my @even;
for (@numbers) {
if ($_ % 2 == 0) {
push @even, $_;
}
}


Once you get used to the idea of functions accepting other functions
(or blocks) as parameters.

Jenda
===== Jenda@xxxxxxxxxxx === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery

.



Relevant Pages

  • Re: reverse list
    ... >would make it possible to find all of the words in English that end in ... Or (still Unix) don't sort, ... grep 'ly$' file ...
    (sci.lang)
  • Re: [opensuse] Re: OpenSUSE 10.3 benchmarking: slower than 10.2?
    ... On Sunday 14 October 2007 03:40:21 jdd wrote: ... It's nothing to do with English, it's just that things like grep are ... It would be great to fix it, ...
    (SuSE)
  • Re: bang methods
    ... find_all grep sort_by ... It's a real word and I know what it means. ... It's certainly more English than extname, callcc, nitems(), etc. ...
    (comp.lang.ruby)