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

Re: [MacPerl] defining variables if not already defined



On Mon, 24 Jul 2000 17:01:30 -0700, Tom Sackett wrote:

>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.

Then do it in another way. For example, ALWAYS declare the variable $var
before running the external code. Let that code set the variable, but
not declare it. Then you can still set it if needed.

Varaibles declared with my() inside an eval STRING, are limited to that
string. It won't affect your code.

	{
		my $var = "original";
		eval 'my $var = "override";';
		print "$var\n";
	}
-->
	original


Gee, that ain't what you wanted.

	{
		my $var;
		eval '$var = "override";';
		$var = 'default' unless defined $var;
		print "$var\n";
	}
-->
	override

That's better.

-- 
	Bart.

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