NAME
Perl::Critic::Policy::ValuesAndExpressions::LiteralSpecialLiteral - specials like __PACKAGE__ used literally
DESCRIPTION
This policy is part of the Perl::Critic::Pulp addon. It picks up some cases where the special literals __FILE__
, __LINE__
and __PACKAGE__
are used with =>
or as a hash subscript and don't expand to the respective filename, line number or package name.
my $seen = { __FILE__ => 1 }; # bad
$obj->{__PACKAGE__}{myextra} = 123; # bad
Here you end up with the string "__FILE__"
or "__PACKAGE__"
, like
my $seen = { '__FILE__' => 1 };
$obj->{'__PACKAGE__'}->{'myextra'} = 123;
whereas you almost certainly wanted to expand to the filename or package name. On that basis this policy is under the "bugs" theme (see "POLICY THEMES" in Perl::Critic).
$obj->{__PACKAGE__}
can arise when you're trying to hang extra data on an object, using your package name to hopefully avoid clashes with the object's native fields. An unexpanded __PACKAGE__
like this is a mistake you'll probably only make once, after that the irritation of writing extra parens or similar will keep it fresh in your mind!
$obj->{(__PACKAGE__)}->{myfield} # good
$obj->{__PACKAGE__.'.myfield'} # good
Don't forget that it's any time a word is immediately to the left of a =>
that it's quoted, so even in expressions like the following __FILE__
and __PACKAGE__
are still not expanded,
my $hash = { 'Foo'.__FILE__ => 123 }; # bad
return ('MyExtra::'.__PACKAGE__ => 123); # bad
If you really do want the string "__FILE__"
etc then the suggestion is to write the quotes, even if you're not in the habit of using quotes in hash constructors. It'll pass this policy and make it clear to everyone that you really did want the string, not an expanded name.
SEE ALSO
Perl::Critic::Pulp, Perl::Critic, "Special Literals" in perldata
HOME PAGE
http://www.geocities.com/user42_kevin/perl-critic-pulp/index.html
COPYRIGHT
Copyright 2008 Kevin Ryde
Perl-Critic-Pulp is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version.
Perl-Critic-Pulp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with Perl-Critic-Pulp. If not, see http://www.gnu.org/licenses.