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

Re: [MacPerl] defining variables if not already defined



At 5:01 PM -0700 7/24/00, Tom Sackett wrote:
>Paul-
>
>Thanks for the info. I was aware that that, in effect, I was "my"ing a
>variable twice in the same scope. However, what I don't understand is why
>the second "my"ing was executed.
>
>The sample code I provided was really just an illustration of the problem.
>In the real-life code, that first line "my ($var) = "bongo";" sometimes
>exists and sometimes doesn't. It's part of code that is generated on the
>fly by another script. That's why I added the condition in line 3; I want
>to define the "my" variable only if it is not already defined.

I'm not that familiar with perl internals, but probably because my 
happens at compile time.  So it set up two variables, both named 
$var.  You see the first until the second declaration, then you see 
the second.  The second is never initialized because the first $var 
is the one that's used in the "defined $var" to decide whether or not 
to initialize the second.

Sound's like you should be using global or package variables instead 
of lexicals, localized if need be.

>
>- Tom
>

-- 
--
Paul Schinder
schinder@pobox.com

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