in reply to Re^3: Stupid If Question
in thread Stupid If Question

(Blocks which aren't even mutually exclusive, I might add.)

Initially I thought you meant that the blocks aren't mutually exclusive from the perspective of lexical variable scoping. It has never occurred to me that an if statement might create a single scope, rather than one for each branch. Testing seems to confirm my assumption that each branch is a separate scope.

#!/usr/local/bin/perl use strict; use warnings; foreach (1..3) { if($_ < 2) { my $x = 1; print "$_ < 2, \$x = $x\n"; } else { print "$_ >= 2, \$x = $x\n"; } } __END__ Global symbol "$x" requires explicit package name at ./test.pl line 10 +. Execution of ./test.pl aborted due to compilation errors.

Then it occurred to me that you probably mean that the conditions of the two if statements are not mutually exclusive - a possible problem with program logic but not affecting variable scope.

Replies are listed 'Best First'.
Re^5: Stupid If Question
by ikegami (Patriarch) on Aug 19, 2009 at 14:44 UTC

    Then it occurred to me that you probably mean that the conditions of the two if statements are not mutually exclusive

    Correct.