Ovid <[EMAIL PROTECTED]> writes: > Apocalypse 12 has the following to say about roles and trust > (http://www.perl.com/pub/a/2004/04/16/a12.html?page=10) > > It's not clear whether roles should be allowed to grant > trust. In the absence of evidence to the contrary, I'm > inclined to say not. > > In Perl 5, I recently found myself in the annoying position of having a > method which could accept scalar, array, or hash references. My > primary code looked similar to this (simplified for clarity): > > sub _attributes { > my ($self, $attrs) = @_; > return $$attrs if UNIVERSAL::isa( $attrs, 'SCALAR' ); > > my @attributes = UNIVERSAL::isa( $attrs, 'HASH' ) > ? %$attrs : @$attrs; > return unless @attributes; > # more code here > } > > This was a private method, but $attrs is an argument that is passed in > by the person using my class. It would be nice if I could just assign > an "attributes" role to the SCALAR, ARRAY, and HASH classes and say > that only my class could see the method(s) it provides. Thus, my > caller would be blissfully unaware that I am doing this: > > $attrs->attributes; # even if it's an array reference
How about: my method SCALAR::attributes($self:) { $$self } my method HASH::attributes(%self:) { %self.kv } my method ARRAY::attributes(@self:) { [EMAIL PROTECTED] } method _attributes($attrs) { my @attributes = $attrs.attributes return @attributes[0] if @attributes == 1; ... } Assuming it's legal. -- Piers Cawley <[EMAIL PROTECTED]> http://www.bofh.org.uk/