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

Re: [MacPerl] defining variables if not already defined



Does my() always operate at compile time? Does that mean that my hopes 
for easy run-time allocation must be dashed?

I know, I know, run-time allocation is almost invariably related to 
lists, hashes, and automatic array re-sizing. 

But what about this?

# The following definition might be in external source.
my $pig = atan2( 1, 1 ) * 4;
# . . .
# Then we might use the full pig somewhere:
print "full pig is $pig\n";
# . . .
# But,
unless ( !defined( $pig ) )
{    my $pig = 3.1416;
     print "extemporaneous pig is $pig\n";
     # Use this special definition of pig in something
     # that you only want done if the full pig is unavailable.
     # . . .
}
# . . .
# and later on,
{    my $pig = "three little";
     print "dramatic pig is $pig\n";
     # Use a hidden definition of pig and discard it.
     # . . .
}
# Then we might use the full pig somewhere else.
print "full pig is still $pig\n";
# . . .

My results as follows, with -w switch, no compile or run-time warnings:

# Untitled syntax OK
full pig is 3.14159265358979
dramatic pig is three little
full pig is still 3.14159265358979

No surprises. Valid syntactically and semantically because the enclosing 
blocks allow it all to happen at compile time, right? 

And someone mentioned using eval, so I could stretch this example and get 
a my variable allocated and disposed of at compile time at run time, too? 
(If I really wanted to.)

Or do I over-interpret the assertion that my() does its stuff at compile 
time? This is an important issue for me, because, when I start coding 
full apps, I want to be able to depend (for natural version control and 
similar things) heavily on distinguishing compile-time from run-time, and 
on sometimes postponing compile-time events.

Oh. One other thing (since it was mentioned in relation to this topic): 
Why doesn't the preferences panel for MacPerl include a default switches 
panel. Then (maybe?) I wouldn't have to go to the script menu twice 
(warnings and taint) every time I re-launch MacPerl. Am I missing 
something? The flags on the shebang line are only compared against (and 
complained about).

On  Mon, 24 Jul 2000  Ronald J Kimball wrote:
>
>On Mon, Jul 24, 2000 at 07:45:08PM -0400, Paul Schinder wrote:
>> At 4:14 PM -0700 7/24/00, Tom Sackett wrote:
>> >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.
>
>my has a compile time effect and a runtime effect, so in this code:
>
>my($var) = "conga" unless (defined($var));
>
>the compile-time effect of lexicalizing $var occurs, *regardless* of the
>value of $var at runtime.
>
>This is an "undocumented feature".  :)
>
>So, don't put a statement modifier on my() unless you know what you're
>doing.
>
>Ronald


rees_joel@fujicomp.co.jp
http://www.fujicomp.co.jp
http://www.udit.gr.jp


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