[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] defining variables if not already defined



Now I'm confused. I thought that "my $test = 1 if 0;" would lexically 
scope $test to foo alone but it appears that only happens if "my 
$test = 1" is executed.

What am I missing?

Stewart


>On Fri, 28 Jul 00 12:28:59 +0900, Joel Rees wrote:
>
>  >Does my() always operate at compile time? Does that mean that my hopes
>  >for easy run-time allocation must be dashed?
>
>my() has both a compile time, and a runtime effect.
>
>At compile time, it takes care of scoping issues. So, you cannot
>possibly use a variable in fixed code, that is allocated at runtime in
>eval STRING. The mere fact that a variable is accessed, makes sure that
>it is allocated, so these would refer to different variables.
>
>At runtime, it takes care of storage allocation.
>
>An example of the confusing side effects can be seen in:
>
>	$\ = "\n";
>	print foo(10);
>	print foo(20);
>	print foo(10);
>
>	sub foo {
>	    my ($comp) = @_;
>	    my $test = 1 if 0;
>	    $test = 2 if $comp == 20;
>	    $test || 3;
>	}
>
>This prints:
>
>	3
>	2
>	2
>
>So, don't do this.
>
>In summary: you needn't worry about compile time allocation. You need
>the declaration at compile time, to take proper care of scoping. But
>only the reference to the variable is allocated until runtime, and we're
>talking about a few tens of bytes per variable. Memory allocation for
>the value only happens when you assign something to it that isn't undef,
>and even that is in the same region. Allocation is only a worry if you
>get really large scalar values, i.e. starting at a few tens of k,
>depending on how many variables there are.
>
>--
>	Bart.
>
># ===== Want to unsubscribe from this list?
># ===== Send mail with body "unsubscribe" to macperl-request@macperl.org

---
Stewart Leicester              |  "Ad Astra Per Aspera"
JenSoft Technologies           |  <http://www.id2k.com>
<mailto:StewartL@JenSoft.com>  |  Guest passcode: 1069340

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org