[Israel.pm] Doing it the perlish way

Yossi.Itzkovich at ecitele.com Yossi.Itzkovich at ecitele.com
Thu Jun 8 11:47:35 PDT 2006


Thanks Yuval for the fast response, but I have few comments:

perl-bounces at perl.org.il wrote on 08/06/2006 21:29:39:

...
> # i removed -w and use these instead:
>
> use strict;
> use warnings;

I don't think it's more perlish, but anyhow if I don't need the advantages
of use warnings (such as "no warnings") I think that -w is better.
...
> # i prefer 3-argument open and lexical file handles
> open my $dict_fh, "<", $dict or die "can't open($dict): $!"; # tell them
why we can't open it
Why  do you prefer 3-arguments open ?
>
> my @dict = <$dict_fh>;
> chomp for @dict; # trim the newlines
What for ?

> close $dict_fh; # it's nice to close once we're done, when you read
> # the code you know it won't be used afterwords so things are
> # clearer
I agree it's nice, and it's "descriptive writing", but especially in Perl
the common way is not to close file handles, unless needed.
>
> $\ = "\n";
> $, = ", ";
>
> while (<>) { # if it's not <STDIN> we allow patterns from files too
>     chomp;
>    my $pat = qr/$_/; # qr is both faster and more informative
Why is it faster, and why more informative ?
>     print STDERR "pat=$pat"; # go to STDERR so that it doesn't wind  up
in pipes
I agree.  In this case why not using warn ?
>     print grep /$pat/, @dict; # $, and $\ play a nice part here
> }


Thanks

Yossi




More information about the Perl mailing list