in reply to Declaring a lexical within a 'if' condition

You've created an unnecessarily complex example to replicate your observation - you get the same error with:

use strict; (my $x = 15) && $x;

and obviously the following does not throw the error:

use strict; (my $x = 15);$x;

Unfortunately I don't have a good why scoping behaves the way it does in these examples - I would naively expect the my statement to execute before the second term in the conditional was evaluated. I found the documentation you want to read. From Private Variables via my() in perlsub:

The declared variable is not introduced (is not visible) until after the current statement.

So there you go. I assume the design logic is so the following works-as-hoped:

my $x = 1; for (1 .. 5) { my $x = $x; $x += $_; print $x; } print $x;

Replies are listed 'Best First'.
Re^2: Declaring a lexical within a 'if' condition
by LanX (Saint) on Mar 03, 2010 at 16:33 UTC
    > The declared variable is not introduced (is not visible) until after the current statement.

    exactly, it's meant to allow constructs like my $x=f($x) where the new lexical can be initialized with help from the old scope!

    Cheers Rolf