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