develooper Front page | perl.perl6.language | Postings from September 2001

Re: What's up with %MY?

Thread Previous | Thread Next
From:
Damian Conway
Date:
September 4, 2001 15:53
Subject:
Re: What's up with %MY?
Message ID:
200109042252.JAA07823@indy05.csse.monash.edu.au

Dave Mitchell asked:
 
   > If there is to be a %MY, how does its semantics pan out?

That's %MY::. The colons are part of the name.

   
   > for example, what (if anything) do the following do:
   > 
   > sub Foo::import {
   >     my %m = caller(1).{MY}; # or whatever
   >     %m{'$x'} = 1;
   > }

That would be:

     sub Foo::import {
         my $m = caller(1).{MY};
         $m{'$x'} = \1;
     }

Symbol table entries store references (to the actualy storage), not values.
My above example would make the lexical $x variable in the caller's scope
equivalent to:

	my $x : const = 1;

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

That would be:

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

which would cause the next attempted access to the lexical $x in the
caller's scope to throw an exception.


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

$x becomes constant, with value 1.

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

Yes.

   > 	# or is the outer $x modified?

No.

   > 	use Foo;
   > 	...
   >     }

Because %MY:: is lexical to each scope.

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

No.

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

Run-time exception.


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

No.


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

No difference between compile- and run-times.
Effects:

	What				 Effect

	add entry to %MY::		 Creates new lexical in scope
	modify entry in %MY::		 Changes implementation of lexical
	delete entry in %MY:		 Prematurely removes lexical from scope
	existence test entry in %MY::	 Does lexical exist in scope?
	definition test entry in %MY::	 Is lexical implemented in scope?

Damian

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About