From: Dave Mitchell [mailto:[EMAIL PROTECTED]]
> 
> If there is to be a %MY, how does its semantics pan out?
> 
> for example, what (if anything) do the following do:
> 
> sub Foo::import {
>     my %m = caller(1).{MY}; # or whatever
>     %m{'$x'} = 1;
> }

IMO: Sets the value of the lexical $x in the caller's scope to 1,
autovifying '$x' if it doesn't exist.

 
> sub Bar::import {
>     my %m = caller(1).{MY}; # or whatever
>     delete %m{'$x'};
> }

hmm... when:

{ my $x = 1; sub incr {$x++} }

is compiled, the $x++ in &incr refers to the lexical $x. So deleting it
would remove it from the scratchpad of &incr. But I would guess that future
calls to &incr would have to autovify $x in the scratchpad and start
incrementing it from 0. I.e., ignoring a package $x if it exists. I could
see people prefering it either way...


> sub f {
>     my $x = 9;
>     use Foo; # does $x become 1, or $x redefined, or runtime 
>              # error, or ... ?

do you mean Foo::import()? 'use' is handled like:

BEGIN {
  require Foo;
  Foo::import(@_);
}

So 'use Foo' would modify the caller, which being processed at compile time
would be 'main'. So this would create a my $x in the scope of the main_cv.
Which would then be removed by the later 'use Bar'.

Assuming Foo::import(), I would guess that the $x which is local to &f would
be assigned the value 1.


>     {
>       # is this the same as 'my $x = 1' at this point,
>       # or is the outer $x modified?

If you indeed meant 'use Foo', then the lexical $x of 'main' would be
created and set to 1.

Or, assuming you meant Foo::import() the answer would be neither. It would
neither modify the outer $x or create a new my $x, but modify the value of
the $x which exists within the scope of &f.


>       use Foo;
>       ...
>     }
>     use Bar; # is $x now still in scope?

'use Bar' would occur at compile time, and would remove the $x from main's
lexical scratchpad which had been created when you did 'use Foo'.

Or had you said Bar::import(): My guess would be that at this point, $x
would be removed from the stash of &f.


>     print $x; #compile error? or runtime error? or prints 9 
>               #(or 1...) ????

If you used 'use Foo' and 'use Bar', it would print 9. Because the $x local
to &f would never have been touched.

If you meant Foo::import() and Bar::import() and had warnings turned on, it
would print:

  Use of uninitialized value in print at ...

     
>     Bar::import(); # any difference calling it at run time?

Yes... as mention above. One happens at compile, the other at runtime so the
caller and consequently lexical scope is different in each case.


> }
> 
> and so on....
> 
> IE what effects to do the standard hash ops of adding, modifying,
> deleting, testing for existence, testing for undefness, etc 
> etc map onto when applied to some sub's %MY, at either compile
> or run time.

I would hope that it would be identical to the current behavior we
experience when modifying a package's stash. Or however the new behavior for
stashes maps to Perl6.

Reply via email to