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

Re: [MacPerl] defining variables if not already defined



Hi Tom,

Rule #1 in debugging is to use -w.

#!/usr/local/bin/perl -w

Second, your use of 'my' is not quite correct. If you had turned on the
warning you would have seen a message like this:

File untitled; Line 5: "my" variable $var masks earlier declaration in same
scope.

Oops.

According to Shuck: A "my" declares the listed variables to be local
(lexically) to the enclosing block, subroutine, eval, or do/require/use'd
file.  If more than one value is listed, the list must be placed in
parentheses.  See the section on Private Variables via my() in the perlsub
manpage for details.

Rewrite your program like this:

#!/usr/local/bin/perl -w

 my ($var) = "bongo";
 print "The variable is: $var\n";
 
 $var = "conga" unless (defined($var));
 print "The variable is now: $var\n";

and your results should be:

The variable is: bongo
The variable is now: bongo

I hope that helps.

Elton

=========================================================================
NOVA Career Connection                        505 W. Olive Ave. Suite 550
Elton Hughes (Information Technology)                  Sunnyvale CA 94086
Phone: 408-730-7235                                     Fax: 408-730-7643
-------------------------------------------------------------------------




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