warnings::pedantic - Dubious warnings for dubious constructs.

VERSION

Version 0.02

SYNOPSIS

This module provides a pedantic warning category, which, when enabled, warns of certain extra dubious constructs.

use warnings::pedantic;

grep { ... } 1..10; # grep in void context
close($fh);         # close() in void context
print 1;            # print() in void context

DESCRIPTION

Besides the pedantic category, which enables all of the following, the module also provides separate categories for individual groups of warnings:

  • void_grep

    Warns on void-context grep:

    grep /42/, @INC;
    grep { /42/ } @INC;

    This code is not particularly wrong; it's merely using grep as an alternative to a foreach loop.

  • void_close

    Warns on void-context close() and closedir():

    close($fh);
    closedir($dirh);

    This is considered dubious behaviour because errors on IO operations, such as ENOSPC, are not usually caught on the operation itself, but on the close() of the related filehandle.

  • void_print

    Warns on void-context print(), printf(), and say():

    print();
    say();
    printf();
  • sort_prototype

    Warns when sort()'s first argument is a subroutine with a prototype, and that prototype isn't $$.

    sub takes_a_block (&@) { ... }
    takes_a_block { stuff_here } @args;
    sort takes_a_block sub {...}, @args;

    This probably doesn't do what the author intended for it to do.

  • ref_assignment

    Warns when you attempt to assign an arrayref to an array, without using parenthesis to disambiguate:

    my @a  = [1,2,3];   # Warns; did you mean (...) instead of [...]?
    my @a2 = ([1,2,3]); # Doesn't warn

    This is a common mistake for people who've recently picked up Perl.

  • maybe_const

    Identifiers used as either hash keys or on the left hand side of the fat comma are always interpreted as barewords, even if they have a constant attached to that name:

    use constant CONSTANT => 1;
    my %x = CONSTANT => 5;      # Used as "CONSTANT"
    $x{CONSTANT} = 5;           # Ditto

    This is intended behaviour on Perl's part, but is an occasional source of bugs.

Or in tree form:

all -+
     |
     +- pedantic --+
                   |
                   +- void_grep
                   |
                   +- void_close
                   |
                   +- void_print
                   |
                   +- sort_prototype
                   |
                   +- ref_assignment
                   |
                   +- maybe_const
                   
                   

All of the warnings can be turned off with

no warnings 'pedantic';

as well as

no warnings;

or even

no warnings::pedantic;

Additionally, you can turn off specific warnings with

no warnings 'void_grep';
no warnings 'void_close';
no warnings 'void_print'; # printf, print, and say
#etc

AUTHOR

Brian Fraser, <fraserbn at gmail.com>

BUGS

Please report any bugs or feature requests to bug-warnings-pedantic at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=warnings-pedantic. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

ACKNOWLEDGEMENTS

The warning for void-context grep was at one point part of the Perl core, but was deemed too controversial and was removed. Ævar Arnfjörð Bjarmason recently attempted to get it back to the core as part of an RFC to extend warnings.pm, which in turn inspired this module.

LICENSE AND COPYRIGHT

Copyright 2014 Brian Fraser.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.