Re: Question about Watcher Attributes

[email protected] (Nick Ing-Simmons)
Newsgroups perl.loop
Organization via, but not speaking for : Texas Instruments Ltd.
Message-ID <[email protected]>
Uri Guttman <[email protected]> writes:
>>>>>> "JNP" == Joshua N Pritikin <[email protected]> writes:
>
>  >> sub privateData
>  >> {
>  >> my $obj = shift;
>  >> my $key = shift || caller;
>  >> $obj->{$key} ||= {};
>  >> }
>
>
>i am not sure of why the need for private data per caller. 

It was necessary in Tk when different classes were all adding 
functionality to one widget.

e.g. in the web browser stuff one class was storing URL that was 
associated with the "Text", and another class was storing "style"
information.

>i used the
>data attribute in a global way when i wrote event loops.



>
>
>  JNP> sub data {  # assumes $self is a HASH ref
>  JNP>     my $self = shift;
>  JNP>     my $pkg = caller;
>  JNP>     if (@_) {
>  JNP> 	$self->{$pkg} = shift
>  JNP>     } else {
>
>drop the else. accessors should always return the value whether freshly
>set or not.

Which is just what this does:

  if (@_) {
	$self->{$pkg} = shift  # set the element and returns it 
    } else {
	$self->{$pkg};         # just return it.
    }

>
>	$self->{$pkg} = shift if @_ ;
>	$self->{$pkg};

That does the hash lookup twice for the set case, while the else form 
only does it once.

-- 
Nick Ing-Simmons <[email protected]>
Via, but not speaking for: Texas Instruments Ltd.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.