[Israel.pm] Doing it the perlish way

Gabor Szabo szabgab at gmail.com
Thu Jun 8 21:19:07 PDT 2006

On 6/8/06, Yossi.Itzkovich at ecitele.com <Yossi.Itzkovich at ecitele.com> wrote:
> > # 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.

In scripts I tend to use -w, in modules use warnings;

> ...
> > # 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 ?

If coming from a user $dict might contain ">/etc/passwd" or similar
and then open (FH, $dict) might do a major harm. In your case, in the
current version of your script $dict is hard coded in the script so it
is not necessary
to use the 3 param version but
I prefer not to think each time I open a file if I need the extra caution or not
I'd rather get into habit of using open with 3 params. (Hmm, there should be
even a use forceopen3;  or a Test::Open3 that would check for this.)

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

Now that we have scalar file handlers it is nice that they get closed when
they go out of scope. In this code they only go out of scope at the end of
the script. In such cases I think it is clearer to close the file. Again in this
short script you don't really need closing the filehandler but it is a good
habbit to get used to.

At least that's what I teach, even if I am not always doing it that way :-)


More information about the Perl mailing list