Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re^3: Announcing Perl-Critic-0.14

by bart (Canon)
on Jan 30, 2006 at 16:47 UTC ( #526484=note: print w/ replies, xml ) Need Help??

in reply to Re^2: Announcing Perl-Critic-0.14
in thread Announcing Perl-Critic-0.14

Please forgive my ignorance, but what's wrong with it? $foo ends up as 1 when $bar is true and is undefined when $bar is false. Just as I expect it to be. (Active Perl 5.8.7)
Wrong. This piece of code is one of the weird things in perl, in this case, something that is sometimes used as a dirty trick, to create a static variable.
#!/usr/bin/perl -lw print $]; for (1 .. 5) { my $i = 123 if $bar; print ++$i; }
Name "main::bar" used only once: possible typo at line 4. 5.008003 1 2 3 4 5

Comment on Re^3: Announcing Perl-Critic-0.14
Select or Download Code
Replies are listed 'Best First'.
Re^4: Announcing Perl-Critic-0.14
by holli (Monsignor) on Jan 30, 2006 at 17:26 UTC
    Conclusion: Always assign something in a my declaration?

    The following works as expected.
    my $i = $bar ? 123 : undef;
    And yeah, that's really weird.

    holli, /regexed monk/

      Conclusion: Always assign something in a my declaration?

      More precisely: make sure an assignment with a my on the left-hand side is never conditional.

      Makeshifts last the longest.

        It's not about the assignment. It's that my() should never be conditional. my $foo if $bar is just as bad for the same reason. You're just not likely to write that because it looks funny. my $foo ... for ... is probably also just as bad. The statement modifier form allows the my() to declare the variable for use by all following statements in that scope.

        ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

      Always assign something in a my declaration?
      I think it boils down to: Never modify a lexical unless the my() was executed during the current execution of the enclosing block.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://526484]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2015-11-29 02:50 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (746 votes), past polls