in reply to (tye)Re: Why no stay shared warning?
in thread Why no stay shared warning?

It seems that my $foo only creates a place for sub up to store values between calls. Why does initializing it to 20 not have an effect?
Thanks
use strict; use diagnostics; up(); up(); print "getfoo=",get(); { my $foo = 20; sub up { print "subup=$foo\n"; $foo++; sub get {$foo} } } prints: subup= subup=1 getfoo=2

Replies are listed 'Best First'.
Re: Re: (tye)Re: Why no stay shared warning?
by japhy (Canon) on May 05, 2001 at 20:01 UTC
    The reason is because $foo = 20 doesn't occur until after you've called all those functions.

    DECLARATION occurs at compile-time. ASSIGNMENT happens at run-time. Different things. my $foo = 20 has a compile-time effect (my) and a run-time effect ($foo = 20).

    For your desired action, you'd need something like:
    my $foo; BEGIN { $foo = 20 }


    japhy -- Perl and Regex Hacker