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.

Reply via email to