[Html-widget] attributes

Carl Franks fireartist at gmail.com
Thu Sep 28 18:04:06 CEST 2006


On 22/09/06, A. Pagaltzis <pagaltzis at gmx.de> wrote:
> * Carl Franks <fireartist at gmail.com> [2006-09-22 16:30]:
> > On 22/09/06, A. Pagaltzis <pagaltzis at gmx.de> wrote:
> > >* Carl Franks <fireartist at gmail.com> [2006-09-22 14:50]:
> > >> Setting attributes in this way overwrites on the current
> > >> attributes hash, and wipes out the 'size' which internally
> > >> is saved as an attribute. Not good.
> > >
> > >So save `size` beforehand and wedge it back in after assigning
> > >the new hash.
> >
> > I'm worried about breaking backward compatability.
>
> Yeah, d'oh. Then only add `size` back in if it doesn't already
> exist in the fresh hash, so that you don't overwrite explicitly
> set values.
>
> > Should the current behaviour be considered a bug, and we add a
> > new method to explicitly delete the current attributes?
>
> If you can accept breaking compatibility like that, then I would
> say the right course of action is to switch the behaviour from
> replacing to merging hashes. After all, the very fact that we're
> having this discussion proves that people find the current
> non-merging behaviour surprising.
>
> There is no need for another method if you do so, as I said. You
> can already explicitly delete the current attributes:
>
>     %{ $w->attributes } = ( disabled => 'disabled' );
>
> This even documents intent clearly.

Is this a reasonable summary?

If attributes() receives 1 argument, it must be a hash-ref.
The key/value pairs are added to the current attributes.
Return value is $self to allow method-chaining.

This means the following will still work:
$w->attributes( { $key => $value } );
$w->attributes( \%attrs );

This will be break:
$w->attributes( {} );
It will no longer empty the attributes hash-ref.
This is used internally, but can be changed. Hopefully no-one else is
using this technique.

If anyone is using the return value from $w->attributes(\%attrs), that
will also break, as it will now return $w instead of the \%attrs.

If attributes() receives multiple arguments, they are added to the
attributes hash.
Return value is $self.

$w->attributes( %attrs );

Special behaviour for backwards compatability:
If attributes() receives no arguments, it returns a hash-ref of attributes.

This means the following will still work:
$w->attributes->{$key} = $value;

This can now be used to clear the hash:
%{ $w->attributes } = ();
To clear the hash and assign new values:
%{ $w->attributes } = ( $key => $value );

Thanks,
Carl



More information about the Html-widget mailing list