On Wed, Nov 27, 2002 at 04:41:13AM +0000, Paul Makepeace wrote:
> $ perl -Mstrict -le 'print "one" if (my $d = "1") && $d'
>
> [or indeed if ((my $d = "1") && $d) {...} ]
>
> perl apparently doesn't consider $d exists by the second $d and issues
> an error. Can someone explain this? (Esp. in light of all this sequence
> point talk.)
>
> perldoc perlsub says,
>
> The "my" operator declares the listed variables to be lexically con-
> fined to the enclosing block, conditional ("if/unless/elsif/else"),
> loop ("for/foreach/while/until/continue"), subroutine, "eval", or
> "do/require/use"'d file.
>
> This doesn't sound to me like it unequivocably explains the above.
Keep reading... :)
The declared variable is not introduced (is not visible)
until after the current statement. Thus,
my $x = $x;
can be used to initialize a new $x with the value of the
old $x, and the expression
my $x = 123 and $x == 123
is false unless the old $x happened to have the value
"123".
> Any suggestions/hacks how to get around this gratefully received: I
> have a nasty chain of elsifs and I'd like to get a tmp variable into
> the last conditional without declaring it miles away... (or re-writing
> the logic, heh :-)
I'm not sure from the example you provided exactly what you're trying to
do. Could you give another example?
Ronald