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. - Tom At 07:45 PM 7/24/00 -0400, Paul Schinder wrote: >At 4:14 PM -0700 7/24/00, Tom Sackett wrote: >>I'm having a problem with the following code: >> >> my ($var) = "bongo"; >> print "The variable is: $var\n"; >> my ($var) = "conga" unless (defined($var)); >> print "The variable is now: $var\n"; >> >>I would expect that this script would produce: >> The variable is: bongo >> The variable is now: bongo >> >>However, what I get is the following: >> The variable is: bongo >> The variable is now: >> >>The variable $var is losing its value, apparently because the first part of >>line 3 is being partially executed (as if I had said "my ($var);" with no >>assignment). I would expect that the first part of line 3 would not get >>executed, because the condition (defined($var)) would evaluate as TRUE. >> >>The same thing happens no matter how I rewrite line 3, I've tried the >>following: >> (my ($var) = "conga") unless (defined($var)); >> my ($var) = "conga" if (!defined($var)); >> (defined($var)) || (my ($var) = "conga"); >> >>Any idea what's going on, or what I can do about it? > >It *did* give you an warning message, didn't it? > ># "my" variable $var masks earlier declaration in same scope. >File 'Untitled'; Line 3 >The variable is: bongo >The variable is now: > >That's what's going on. You're "my"ing a variable twice in the same >scope, which is A Bad Thing. > >Try > > my $var = "bongo"; > print "The variable is: $var\n"; > $var = "conga" unless (defined($var)); > print "The variable is now: $var\n"; > >Or even better: > >#!perl -w >use strict; > my $var = "bongo"; > print "The variable is: $var\n"; > $var= "conga" unless (defined($var)); > print "The variable is now: $var\n"; > > >> >>- Tom Sackett >> tsackett@iname.com >> > >-- >-- >Paul Schinder >schinder@pobox.com > > # ===== Want to unsubscribe from this list? # ===== Send mail with body "unsubscribe" to macperl-request@macperl.org