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

[MacPerl] defining variables if not already defined



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?

- Tom Sackett
  tsackett@iname.com

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