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

Re: [MacPerl] defining variables if not already defined



This is a rather bizarre side effect, but I think what Bart is trying to
clarify is that $test is lexically scoped at runtime by the mere existence
of the "my $test" statement. You can verify this by adding a "print $test"
after the last "foo" call. However, because Perl apparently wants to also do
what you tell it to, it recognizes the "if" statement and tries to not treat
"$test" as a lexically scoped variable, and ends up creating a weird ad-hoc
static scope (vis a vis C). An interesting exploit (if you want a static
scope variable) but probably not one that will survive future revisions (or
will it?).

-K

-- 
Un debauché de profession est rarement un homme pitoyable.
    - De Sade

> From: Stewart Leicester <StewartL@JenSoft.com>
> Date: Fri, 28 Jul 2000 14:24:23 -0700
> To: MacPerl mailing list <macperl@macperl.org>
> Subject: 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
> 


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