> -----Original Message-----
> From: Ivan Adzhubei [mailto:[EMAIL PROTECTED]]
> Sent: Friday, August 10, 2001 7:24 AM
> To: [EMAIL PROTECTED]
> Subject: Scope, priority or bug?
>
>
> Hi!
>
> Isn't this weird?
>
> #!/usr/bin/perl -w
> print("Defined: <<$z>>\n") if defined(my $z="x");
> print "Assigned: <<$z>>\n";
>
> Produces:
>
> Defined: <<>>
> Assigned: <<x>>
>
> So it looks like $z *is* defined but *not* assigned until we
> are out of
> the 'if' statement. Is it a bug? My understanding it should
> either stay
> undefined or defined *and* assigned at the same moment, am I missing
> something?
>
> There's nothing helpful in the documentation, the only short note in
> perlvar admits that whatever more or less detailed description of my()
> scope issues it has for complex statements -- does *not*
> apply to simple
> ones (like the above example). However, description of the scoping
> within simple statements is nowhere to be found :(.
>
> -w switch produces complaints about '"main::z" used only
> once' but this
> is not very helpful, I'd like to know *why* it appears to not being
> properly initialized...
I think the answer can be found in the following statement in the persub
manpage
under the heading "Private Variables via (my)":
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".
So the $z being printed in the first line of your example is the global $z,
not the lexical (my $z).
Adding "use strict;" to the program will highight this fact by complaining
about using the
global $z on the first line.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]