Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Why is it uninitialized?

by hexcoder (Curate)
on Dec 20, 2017 at 21:03 UTC ( [id://1205945]=note: print w/replies, xml ) Need Help??


in reply to Why is it uninitialized?

In [Perl Best Practices] from Damian Conway, there is an entry 'Non-Lexical Loop Iterators' in chapter 6 warning about this construct.

Without my, Perl does not use the variable $S4 for the loop, but instead uses a new lexically variable also named $S4. Thats why the first variable $S4 is uninitialized in sub X.

So your code behaves like this:

#!env perl use strict; use warnings; my $S4; sub X { print "<$S4>\n"; return $S4; } for my $other_variable_also_named_S4 (1 .. 2) { print "A: <$other_variable_also_named_S4> <", X(), ">\n"; }

Conway warns to use this construct since its 'behaviour is contrary to all reasonable expectation'.

BTW: perlcritic warns about it too.

Replies are listed 'Best First'.
Re^2: Why is it uninitialized?
by dsheroh (Monsignor) on Dec 21, 2017 at 08:16 UTC
    BTW: perlcritic warns about it too.

    But, then, that's hardly surprising, given that "Perl::Critic is distributed with a number of Perl::Critic::Policy modules that attempt to enforce various coding guidelines. Most Policy modules are based on Damian Conway's book Perl Best Practices."

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-16 22:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found