Re^4: A curious case of of my() (What is GLOBAL?)
by LanX (Saint) on Apr 05, 2011 at 12:18 UTC
|
As already said my has a compile-time effect of declaring a lexical variable and the run time effect of initialization.
So this variable is a lexical which just isn't resetted in further runs, because of the if condition, (such that the old value of previous runs remains in the assigned memory-slot)
One might argue what a global variable is, in Perl context it's normally used as synonym for package variables.
Actually I'm trying to avoid the term "global" and prefer saying "package" or "lexical"! (There are hacks to globally manipulate lexicals)
IMHO the only acceptable least misunderstandable use of the term "global" is with special vars like $_ which always belong to main:: without full qualification, no matter which package you're in.
$_=666;
package Foo;
print $_; # -> 666
HTH! :)
| [reply] [d/l] [select] |
|
|
$_=666;
{
my $_=777;
print $_; # -> 777
print $main::_; # -> 666
}
(just meaning to say that $_ isn't the best example — otherwise I agree of course) | [reply] [d/l] [select] |
|
|
I'm not sure but IIRC lexical $_ is a rather new feature...
But thanks!
| [reply] |
|
|
|
|
|
|
|
|
hmmm - but separating the my from the if turns on the lexical scope whereas leaving it as is keeps that turned off when tested.
| [reply] |
|
|
Scope is another often misunderstood concept.
For instance my, our and package follow the same scope rules (block or file scope).
The scope decides which declaration is used at compile time too chose a "memory slot" for an (unqualified) variable. And the effect is hardcoded into the compiled opcodes!
(i.e. either a hash-lookup in a "symbol table" of a package or "variable pad" of the surrounding lexical blocks)
You're identifying scope with initialization, but the latter is a run time issue, which is disabled in an if-clause.
Other languages like JS or Python follow more static mechanisms, leading to other quirks.
| [reply] [d/l] [select] |
|
|
| [reply] |
A reply falls below the community's threshold of quality. You may see it by logging in.
|
|
|
| [reply] [d/l] [select] |
Re^4: A curious case of of my()
by JavaFan (Canon) on Apr 05, 2011 at 11:43 UTC
|
I did of course test my assertions before presenting them.
Care to post the test that showed @array is a global variable?
If the array were lexical it would not persist between calls to the subroutine.
That's not the definition of a lexical variable.
All of your points are in fact incorrect
Please show some code that proves my @array if EXPR; generates a global variable. Or how enabling strict and warnings would have pointed out the problem to the OP.
| [reply] [d/l] |
|
|
It's the line "push @array ..." that auto-vivified the global array in the absence of strict declarations - the my did not take effect.
| [reply] |
|
|
| [reply] [d/l] |
Re^4: A curious case of of my()
by ikegami (Patriarch) on Apr 05, 2011 at 15:33 UTC
|
use Devel::Peek qw( Dump );
sub f {
my $x;
Dump($x);
$x = "abc";
}
f();
f();
SV = NULL(0x0) at 0x2cb90dc
REFCNT = 1
FLAGS = (PADMY)
SV = PV(0x1542f64) at 0x2cb90dc
REFCNT = 1
FLAGS = (PADMY)
PV = 0x154b194 "abc"\0
CUR = 3
LEN = 12
You can tell it's the same variable in both calls to the function not only by the address being the same, but by the state left over between calls. Would you care to change you claim that this $x isn't a lexical?
See Re: A curious case of of my() | [reply] [d/l] [select] |