in reply to scope of my $x in if statements

I want the $x in g($x) to reference the lexical $x on the same line.

That's currently not possible in order to allow

my $x = $x; # Initialize with value from outer scope.

The clearest alternative is probably

my $x = f(); if ($x and g($x)) { ... }
or
if (my $x = f()) { if (g($x)) { ... } }

Replies are listed 'Best First'.
Re^2: scope of my $x in if statements
by ig (Vicar) on May 08, 2009 at 01:32 UTC

    The first expands the scope of $x.

      So?

        Just a nit, unless one is concerned that this $x should be out of scope after the end of the if block. The outer scope could have its own $x, for example.