Skip Menu |
 

Preferred bug tracker

Please visit the preferred bug tracker to report your issue.

This queue is for tickets about the Perl-Critic CPAN distribution.

Report information
The Basics
Id: 50894
Status: open
Priority: 0/
Queue: Perl-Critic

People
Owner: Nobody in particular
Requestors: user42 [...] zip.com.au
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)

Attachments
RequireInitializationForLocalVars.output-vars.diff



Received: from la.mx.develooper.com (x1.develooper.com [207.171.7.70]) by diesel.bestpractical.com (Postfix) with SMTP id 9950D19B81D7 for <bug-Perl-Critic [...] rt.cpan.org>; Tue, 27 Oct 2009 21:01:01 -0400 (EDT)
Received: (qmail 3399 invoked by uid 103); 28 Oct 2009 01:01:01 -0000
Received: from x16.dev (10.0.100.26) by x1.dev with QMQP; 28 Oct 2009 01:01:01 -0000
Received: from mailout1-1.pacific.net.au (HELO mailout1.pacific.net.au) (61.8.2.208) by 16.mx.develooper.com (qpsmtpd/0.80) with ESMTP; Tue, 27 Oct 2009 18:00:59 -0700
Received: from mailproxy2.pacific.net.au (mailproxy2.pacific.net.au [61.8.2.163]) by mailout1.pacific.net.au (Postfix) with ESMTP id 4D35850BD2B for <bug-Perl-Critic [...] rt.cpan.org>; Wed, 28 Oct 2009 12:00:55 +1100 (EST)
Received: from blah.blah (ppp2E54.dyn.pacific.net.au [61.8.46.84]) by mailproxy2.pacific.net.au (Postfix) with ESMTP id 7E0E227458 for <bug-Perl-Critic [...] rt.cpan.org>; Wed, 28 Oct 2009 12:00:54 +1100 (EST)
Received: from gg by blah.blah with local (Exim 4.69) (envelope-from <gg [...] zip.com.au>) id 1N2wtj-00068O-Ls for bug-Perl-Critic [...] rt.cpan.org; Wed, 28 Oct 2009 12:00:23 +1100
Delivered-To: cpan-bug+Perl-Critic [...] diesel.bestpractical.com
MIME-Version: 1.0
User-Agent: Gnus/5.110011 (No Gnus v0.11) Emacs/23.1 (gnu/linux)
Subject: RequireInitializationForLocalVars on $@ and $!
X-Spam-Status: No, hits=0.0 required=8.0 tests=
Return-Path: <gg [...] zip.com.au>
X-Spam-Check-BY: 16.mx.develooper.com
X-Original-To: bug-Perl-Critic [...] rt.cpan.org
Date: Wed, 28 Oct 2009 12:00:22 +1100
X-Spam-Level: *
X-Virus-Checked: Checked by ClamAV on 16.mx.develooper.com
Content-Type: multipart/mixed; boundary="=-=-="
Message-ID: <87d448qqmx.fsf [...] blah.blah>
To: bug-Perl-Critic [...] rt.cpan.org
From: Kevin Ryde <user42 [...] zip.com.au>
Content-Length: 0
X-RT-Original-Encoding: ascii
content-type: text/plain; charset="utf-8"
Content-Length: 405
Download (untitled) / with headers
text/plain 405b
In the current perlcritic cvs RequireInitializationForLocalVars reports a violation on local $@; or local $!; where I hoped it might recognise them as output variables not normally given an initializer when localizing. (Localizing to avoid clobbering a caller's value, etc.) A bit of a grep suggests "local $@;" is quite common. $! less so but has the same rationale. Perhaps like below,
Content-Type: text/x-diff
content-disposition: inline; filename="RequireInitializationForLocalVars.output-vars.diff"
Content-Length: 2077
Index: lib/Perl/Critic/Policy/Variables/RequireInitializationForLocalVars.pm =================================================================== --- lib/Perl/Critic/Policy/Variables/RequireInitializationForLocalVars.pm (revision 3692) +++ lib/Perl/Critic/Policy/Variables/RequireInitializationForLocalVars.pm (working copy) @@ -33,6 +33,9 @@ sub violates { my ( $self, $elem, undef ) = @_; + + return if List::MoreUtils::all {_is_output_var($_)} $elem->variables; + if ( $elem->type() eq 'local' && !_is_initialized($elem) ) { return $self->violation( $DESC, $EXPL, $elem ); } @@ -41,6 +44,15 @@ #----------------------------------------------------------------------------- +# variables which are outputs of various things and therefore normally won't +# have initializers in a "local" +my %output_vars = ('$!' => 1, + '$@' => 1); +sub _is_output_var { + my ($str) = @_; + return $output_vars{$str}; +} + sub _is_initialized { my $elem = shift; my $wanted = sub { $_[1]->isa('PPI::Token::Operator') && $_[1] eq q{=} }; @@ -88,7 +100,15 @@ } +=head2 Allowed Forms +An initializer is not required for C<$@> and C<$!> since they're normally +outputs from a block of code or a function. + + local $@; # ok + eval { ... } + + =head1 CONFIGURATION This Policy is not configurable except for the standard options. Index: t/Variables/RequireInitializationForLocalVars.run =================================================================== --- t/Variables/RequireInitializationForLocalVars.run (revision 3692) +++ t/Variables/RequireInitializationForLocalVars.run (working copy) @@ -26,8 +26,22 @@ our $bar our ($foo, $bar); +local $!; +local $@; +local ($!, $@); + #----------------------------------------------------------------------------- +## name mixture of $! and other var +## failures 2 +## cut + +local ($!, $foo); +local ($var, $@); + + +#----------------------------------------------------------------------------- + ## name key named "local" ## failures 0 ## cut
MIME-Version: 1.0 (Apple Message framework v1076)
X-Spam-Status: No, hits=0.0 required=8.0 tests=
In-Reply-To: <rt-3.6.HEAD-3617-1256691679-1898.50894-4-0 [...] rt.cpan.org>
X-Mailer: Apple Mail (2.1076)
References: <RT-Ticket-50894 [...] rt.cpan.org> <87d448qqmx.fsf [...] blah.blah> <rt-3.6.HEAD-3617-1256691679-1898.50894-4-0 [...] rt.cpan.org>
Content-Type: text/plain; charset="utf-8"; delsp="yes"; format="flowed"
Message-ID: <46B67CAF-61CC-4862-8F55-18E3F33123B3 [...] imaginative-software.com>
X-RT-Original-Encoding: utf-8
Received: from la.mx.develooper.com (x1.develooper.com [207.171.7.70]) by diesel.bestpractical.com (Postfix) with SMTP id AAF044D8112 for <bug-Perl-Critic [...] rt.cpan.org>; Wed, 28 Oct 2009 12:40:10 -0400 (EDT)
Received: (qmail 18818 invoked by uid 103); 28 Oct 2009 16:40:10 -0000
Received: from x16.dev (10.0.100.26) by x1.dev with QMQP; 28 Oct 2009 16:40:10 -0000
Received: from mail-bw0-f209.google.com (HELO mail-bw0-f209.google.com) (209.85.218.209) by 16.mx.develooper.com (qpsmtpd/0.80) with ESMTP; Wed, 28 Oct 2009 09:40:08 -0700
Received: by bwz1 with SMTP id 1so1300029bwz.13 for <bug-Perl-Critic [...] rt.cpan.org>; Wed, 28 Oct 2009 09:40:04 -0700 (PDT)
Received: by 10.204.153.220 with SMTP id l28mr8671635bkw.86.1256748004542; Wed, 28 Oct 2009 09:40:04 -0700 (PDT)
Received: from ?10.0.1.4? (c-67-160-236-81.hsd1.ca.comcast.net [67.160.236.81]) by mx.google.com with ESMTPS id d13sm2088938fka.22.2009.10.28.09.40.01 (version=TLSv1/SSLv3 cipher=RC4-MD5); Wed, 28 Oct 2009 09:40:02 -0700 (PDT)
Delivered-To: cpan-bug+Perl-Critic [...] diesel.bestpractical.com
Subject: Re: [rt.cpan.org #50894] RequireInitializationForLocalVars on $@ and $!
Return-Path: <jeff [...] imaginative-software.com>
X-Spam-Check-BY: 16.mx.develooper.com
X-Original-To: bug-Perl-Critic [...] rt.cpan.org
Date: Wed, 28 Oct 2009 09:39:59 -0700
X-Spam-Level: *
To: bug-Perl-Critic [...] rt.cpan.org
Content-Transfer-Encoding: 7bit
From: Jeffrey Thalhammer <jeff [...] imaginative-software.com>
RT-Message-ID: <rt-3.6.HEAD-3617-1256748022-1684.50894-0-0 [...] rt.cpan.org>
Content-Length: 358
Download (untitled) / with headers
text/plain 358b
I'm not exactly sure what you mean by classifying these as "output variables." But near the top of page 79 of PBP, Conway recommends explicitly initializing local vars, even when setting the value to undef. Thus... local $@ = undef; local $! = undef; So if I understand your request correctly, then I think the policy should remain as-is. -Jeff
MIME-Version: 1.0
X-Spam-Status: No, hits=-0.0 required=8.0 tests=SPF_PASS
In-Reply-To: <rt-3.6.HEAD-3617-1256691679-1898.50894-4-0 [...] rt.cpan.org>
References: <RT-Ticket-50894 [...] rt.cpan.org> <87d448qqmx.fsf [...] blah.blah> <rt-3.6.HEAD-3617-1256691679-1898.50894-4-0 [...] rt.cpan.org>
Message-ID: <4AE90BC2.9080304 [...] galumph.com>
Content-Type: text/plain; charset=UTF-8; format=flowed
X-RT-Original-Encoding: utf-8
Received: from la.mx.develooper.com (x1.develooper.com [207.171.7.70]) by diesel.bestpractical.com (Postfix) with SMTP id B7BB24D8040 for <bug-Perl-Critic [...] rt.cpan.org>; Wed, 28 Oct 2009 23:28:07 -0400 (EDT)
Received: (qmail 30662 invoked by uid 103); 29 Oct 2009 03:28:06 -0000
Received: from x16.dev (10.0.100.26) by x1.dev with QMQP; 29 Oct 2009 03:28:06 -0000
Received: from pendrell.textdrive.com (HELO pendrell.textdrive.com) (207.7.108.149) by 16.mx.develooper.com (qpsmtpd/0.80) with ESMTP; Wed, 28 Oct 2009 20:28:05 -0700
Received: from emdaer.local (adsl-69-209-223-204.dsl.chcgil.ameritech.net [69.209.223.204]) by pendrell.textdrive.com (Postfix) with ESMTP id DDB01BB6DC for <bug-Perl-Critic [...] rt.cpan.org>; Thu, 29 Oct 2009 03:28:02 +0000 (GMT)
Delivered-To: cpan-bug+Perl-Critic [...] diesel.bestpractical.com
Subject: Re: [rt.cpan.org #50894] RequireInitializationForLocalVars on $@ and $!
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.22) Gecko/20090605 Thunderbird/2.0.0.22 Mnenhy/0.7.5.0
Return-Path: <perl [...] galumph.com>
X-Spam-Check-BY: 16.mx.develooper.com
X-Original-To: bug-Perl-Critic [...] rt.cpan.org
Date: Wed, 28 Oct 2009 22:28:02 -0500
X-Spam-Level: *
To: bug-Perl-Critic [...] rt.cpan.org
Content-Transfer-Encoding: 7bit
From: Elliot Shank <perl [...] galumph.com>
RT-Message-ID: <rt-3.6.HEAD-3617-1256786897-724.50894-0-0 [...] rt.cpan.org>
Content-Length: 641
Download (untitled) / with headers
text/plain 641b
Kevin Ryde via RT wrote: Show quoted text
> In the current perlcritic cvs RequireInitializationForLocalVars reports > a violation on > > local $@; > > or > > local $!; > > where I hoped it might recognise them as output variables not normally > given an initializer when localizing. (Localizing to avoid clobbering a > caller's value, etc.) > > A bit of a grep suggests "local $@;" is quite common.
How common something is is not relevant. Just because "eval { ... }; if ($@) { ... }" is common, doesn't make it right. Conway wants you to explicitly assign undef to the variable so that the naive reader knows the value after the statement.
MIME-Version: 1.0
X-Spam-Status: No, hits=-0.0 required=8.0 tests=SPF_PASS
In-Reply-To: <rt-3.6.HEAD-3617-1256786897-724.50894-5-0 [...] rt.cpan.org>
References: <RT-Ticket-50894 [...] rt.cpan.org> <87d448qqmx.fsf [...] blah.blah> <rt-3.6.HEAD-3617-1256691679-1898.50894-4-0 [...] rt.cpan.org> <4AE90BC2.9080304 [...] galumph.com> <rt-3.6.HEAD-3617-1256786897-724.50894-5-0 [...] rt.cpan.org>
Message-ID: <4AEE2BCF.2070906 [...] galumph.com>
Content-Type: text/plain; charset=UTF-8; format=flowed
X-RT-Original-Encoding: utf-8
Received: from la.mx.develooper.com (x1.develooper.com [207.171.7.70]) by diesel.bestpractical.com (Postfix) with SMTP id 107FD4D80A8 for <bug-Perl-Critic [...] rt.cpan.org>; Sun, 1 Nov 2009 19:46:13 -0500 (EST)
Received: (qmail 5801 invoked by uid 103); 2 Nov 2009 00:46:13 -0000
Received: from x16.dev (10.0.100.26) by x1.dev with QMQP; 2 Nov 2009 00:46:13 -0000
Received: from pendrell.textdrive.com (HELO pendrell.textdrive.com) (207.7.108.149) by 16.mx.develooper.com (qpsmtpd/0.80) with ESMTP; Sun, 01 Nov 2009 16:46:12 -0800
Received: from emdaer.local (adsl-69-209-229-89.dsl.chcgil.ameritech.net [69.209.229.89]) by pendrell.textdrive.com (Postfix) with ESMTP id 3EE03BB5AD for <bug-Perl-Critic [...] rt.cpan.org>; Mon, 2 Nov 2009 00:46:08 +0000 (GMT)
Delivered-To: cpan-bug+Perl-Critic [...] diesel.bestpractical.com
Subject: Re: [rt.cpan.org #50894] RequireInitializationForLocalVars on $@ and $!
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.22) Gecko/20090605 Thunderbird/2.0.0.22 Mnenhy/0.7.5.0
Return-Path: <perl [...] galumph.com>
X-Spam-Check-BY: 16.mx.develooper.com
X-Original-To: bug-Perl-Critic [...] rt.cpan.org
Date: Sun, 01 Nov 2009 18:46:07 -0600
X-Spam-Level: *
To: bug-Perl-Critic [...] rt.cpan.org
Content-Transfer-Encoding: 7bit
From: Elliot Shank <perl [...] galumph.com>
RT-Message-ID: <rt-3.6.HEAD-3617-1257122784-1687.50894-0-0 [...] rt.cpan.org>
Content-Length: 68
I'm closing this one because this policy works exactly as intended.
MIME-Version: 1.0
X-Spam-Status: No, hits=0.0 required=8.0 tests=
In-Reply-To: <rt-3.6.HEAD-3617-1256748022-1684.50894-6-0 [...] rt.cpan.org> (Jeffrey Thalhammer via's message of "Wed, 28 Oct 2009 12:40:23 -0400")
References: <RT-Ticket-50894 [...] rt.cpan.org> <87d448qqmx.fsf [...] blah.blah> <rt-3.6.HEAD-3617-1256691679-1898.50894-4-0 [...] rt.cpan.org> <46B67CAF-61CC-4862-8F55-18E3F33123B3 [...] imaginative-software.com> <rt-3.6.HEAD-3617-1256748022-1684.50894-6-0 [...] rt.cpan.org>
Message-ID: <87iqdsl8zy.fsf [...] blah.blah>
Content-Type: text/plain; charset="utf-8"
X-RT-Original-Encoding: utf-8
Received: from la.mx.develooper.com (x1.develooper.com [207.171.7.70]) by diesel.bestpractical.com (Postfix) with SMTP id 866BD19B8243 for <bug-Perl-Critic [...] rt.cpan.org>; Mon, 2 Nov 2009 19:59:10 -0500 (EST)
Received: (qmail 30332 invoked by uid 103); 3 Nov 2009 00:59:10 -0000
Received: from x16.dev (10.0.100.26) by x1.dev with QMQP; 3 Nov 2009 00:59:10 -0000
Received: from mailout1-1.pacific.net.au (HELO mailout1.pacific.net.au) (61.8.2.208) by 16.mx.develooper.com (qpsmtpd/0.80) with ESMTP; Mon, 02 Nov 2009 16:59:08 -0800
Received: from mailproxy1.pacific.net.au (mailproxy1.pacific.net.au [61.8.2.162]) by mailout1.pacific.net.au (Postfix) with ESMTP id 74AB75092EB for <bug-Perl-Critic [...] rt.cpan.org>; Tue, 3 Nov 2009 11:59:03 +1100 (EST)
Received: from blah.blah (ppp2183.dyn.pacific.net.au [61.8.33.131]) by mailproxy1.pacific.net.au (Postfix) with ESMTP id AE87A8C16 for <bug-Perl-Critic [...] rt.cpan.org>; Tue, 3 Nov 2009 11:59:02 +1100 (EST)
Received: from gg by blah.blah with local (Exim 4.69) (envelope-from <gg [...] zip.com.au>) id 1N57j7-00006k-TL for bug-Perl-Critic [...] rt.cpan.org; Tue, 03 Nov 2009 11:58:25 +1100
Delivered-To: cpan-bug+Perl-Critic [...] diesel.bestpractical.com
User-Agent: Gnus/5.110011 (No Gnus v0.11) Emacs/23.1 (gnu/linux)
Subject: Re: [rt.cpan.org #50894] RequireInitializationForLocalVars on $@ and $!
Return-Path: <gg [...] zip.com.au>
X-Spam-Check-BY: 16.mx.develooper.com
X-Original-To: bug-Perl-Critic [...] rt.cpan.org
Date: Tue, 03 Nov 2009 11:58:25 +1100
X-Spam-Level: *
To: bug-Perl-Critic [...] rt.cpan.org
From: Kevin Ryde <user42 [...] zip.com.au>
RT-Message-ID: <rt-3.6.HEAD-3617-1257209962-127.50894-0-0 [...] rt.cpan.org>
Content-Length: 759
Download (untitled) / with headers
text/plain 759b
"Jeffrey Thalhammer via RT" <bug-Perl-Critic@rt.cpan.org> writes: Show quoted text
> > I'm not exactly sure what you mean by classifying these as "output > variables."
Variables that you wouldn't normally initialize in a local, because you're not localizing to establish a value but instead to protect the variable from being overwritten in the output of eval or a syscall. Show quoted text
> But near the top of page 79 of PBP,
I didn't notice it was a pbp one. I see it in the themes, but do such ones normally cite a chapter and verse ...? What about an option that would tone it down? Then you can have strict pbp, or a middle ground. (Middle ground supported as I say by some 50-odd places in perl itself doing such a localize, in pretty reasonable fashion, as I understand it.)


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

Please report any issues with rt.cpan.org to rt-cpan-admin@bestpractical.com.