- CONFIGURATION AND ENVIRONMENT
- BUGS AND LIMITATIONS
- BUG REPORTING
- TEST AND QUALITY
- DEPENDENCIES AND REQUIREMENTS
- SEE ALSO
- LICENSE AND COPYRIGHT
Perl::Critic::Policy::InputOutput::ProhibitHighPrecedentLogicalOperatorErrorHandling - prohibits logical error handling in open statements
This documentation describes version: 0.02
This policy has no affiliation
This policy addresses an anti-pattern and possible bug. If you use
open combined with the high precedence logical or operator
|| for error handling.
If the file parameter is pointing to a non-existent file, the use of a high precedence logical operator
||, does not short-cut as expected. This implies that the bug only is present if the file does not exist. If the file exists, but cannot be opened the error handling is not working as expected.
open my $fh, '<', $file || die "Can't open '$file': $!"; # not okay open(my $fh, '<', $file) || die "Can't open '$file': $!"; # okay open my $fh, '<', $file or die "Can't open '$file': $!"; # okay open my $fh, "<$file" || die "Can't open '$file': $!"; # not okay open(my $fh, "<$file") || die "Can't open '$file': $!"; # okay open my $fh, "<$file" or die "Can't open '$file': $!"; # okay
The remedy is to use parentheses for
open or the lower precedence logical operator
Alternatively autodie can be used,
This policy is not configurable at this time. Please see the TODO "section".
Do note that this policy conflicts with the policy:
This distribution holds no known limitations or bugs at this time, please refer to the the issue listing on GitHub for more up to date information.
Please report bugs via GitHub.
This distribution aims to adhere to the Perl::Critic::Policy standards and Perl best practices and recommendations.
This distribution requires:
Perl 5.6.0 syntactically for the actual implementation
Perl 5.14 for developing the distribution, which relies on Dist::Zilla. The features on which this policy relies, where introduced in Perl 5.14, but this does not make for an actual requirement for the policy only the recommendations it imposes.
Carp, in core since Perl 5.
Please see the listing in the file: cpanfile, included with the distribution for a complete listing and description for configuration, test and development.
Ideas and suggestions for improvements and new features are listed in GitHub and are marked as
Please see the issue listing on GitHub
In the blog post Dave demonstrates a very subtle bug, which I think many Perl programmers have been or could be bitten by. But instead of searching through the code as a one time activity, I think this would do better as a Perl::Critic policy, so if the bug a some point was reintroduced in the code base it would be caught by Perl::Critic, if you use Perl::Critic that is - and you do use Perl::Critic right?
Jonas Brømsø (jonasbn) <email@example.com>
Nathan Mills for contributing to this policy, documenting and testing two-argument
Perl::Critic::Policy::InputOutput::ProhibitHighPrecedentLogicalOperatorErrorHandling is (C) by jonasbn 2019-2021
Perl::Critic::Policy::InputOutput::ProhibitHighPrecedentLogicalOperatorErrorHandling is released under the Artistic License 2.0
Please see the LICENSE file included with the distribution of this module