develooper Front page | perl.beginners | Postings from February 2012

Re: Parsing Question

Thread Previous | Thread Next
From:
Jim Gibson
Date:
February 1, 2012 13:05
Subject:
Re: Parsing Question
Message ID:
CB4EE910.17CC6%JimSGibson@gmail.com
On 2/1/12 Wed  Feb 1, 2012  11:24 AM, "jbiskofski" <jbiskofski@gmail.com>
scribbled:

> Your comment about the open statement is OK. I guess it really comes down
> to style. I was trying to write something clear that Brandon could improve
> upon.

It is not simply a matter of style. Because of operator precedence, the
statement

  open my $RR, '<', 'nagios.dat' || die 'unable to open nagios.dat :(';

will be parsed as

  open my $RR, '<', ('nagios.dat' || die 'unable to open nagios.dat :(');

because the precedence of '||' is higher than that of ','. Because the
scalar value 'nagios.dat' is always true, the above statement is equivalent
to the following:

  open my $RR, '<', 'nagios.dat';

In other words, the die will never get executed, regardless of the success
or failure of the open. So you either need to place parentheses around the
open arguments:

  open( my $RR, '<', 'nagios.dat') || die 'unable to open nagios.dat :(';

or use the lower-precedent 'or' operator:

  open my $RR, '<', 'nagios.dat' or die 'unable to open nagios.dat :(';

as Timothy has suggested.

For operator precedence, see 'perldoc perlop'.




Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About