Re: Roles and Trust
Ovid <[EMAIL PROTECTED]> writes: > --- Piers Cawley <[EMAIL PROTECTED]> wrote: >> >> 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. > > Just saw this. Sorry for the late reply. > > At first that gave me the willies, then I noticed the "my" on the > front. I assume because of the "my" on there that this would affect > those data types only locally? That seems like it would be a nice > compromise. > > Is this legal syntax? After some discussion on #perl6, we thought probably not (unless @Larry rules otherwise). However, my multi attributes( Scalar $scalar: ) { $$scalar } my multi attributes( Hash %hash: ) { %hash.kv } my multi attributes( Array @array: ) { [EMAIL PROTECTED] } definitely is legal (though it does rather suffer from the end-weight problem to my way of thinking). -- Piers Cawley <[EMAIL PROTECTED]> http://www.bofh.org.uk/
Re: Roles and Trust
--- Piers Cawley <[EMAIL PROTECTED]> wrote: > > 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. Just saw this. Sorry for the late reply. At first that gave me the willies, then I noticed the "my" on the front. I assume because of the "my" on there that this would affect those data types only locally? That seems like it would be a nice compromise. Is this legal syntax? Cheers, Ovid -- If this message is a response to a question on a mailing list, please send follow up questions to the list. Web Programming with Perl -- http://users.easystreet.com/ovid/cgi_course/
Re: Roles and Trust
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/
Re: Roles and Trust
--- Luke Palmer <[EMAIL PROTECTED]> wrote: > sub _attributes($ref) { > my multi attributes ($scalar) { $$scalar } > my multi attributes (@array) { @array } > my multi attributes (%hash) { %hash } > attributes($ref) > } > > That attributes look suspiciously indentityish. There is some > context magic going on. > > Or doesn't this solve your problem? Offhand, it does solve my problem. It might get more problematic if I needed that method available for those types in a variety of classes, but for right now, I can't see that it would be an issue. Still, I'm not entirely convinced that not allowing roles to assign trust is a good thing. It feels a bit arbitrary. Is there a compelling reason why roles should not do this? Cheers, Ovid -- If this message is a response to a question on a mailing list, please send follow up questions to the list. Web Programming with Perl -- http://users.easystreet.com/ovid/cgi_course/
Re: Roles and Trust
On 10/5/05, Ovid <[EMAIL PROTECTED]> wrote: > 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 sub _attributes($ref) { my multi attributes ($scalar) { $$scalar } my multi attributes (@array) { @array } my multi attributes (%hash) { %hash } attributes($ref) } That attributes look suspiciously indentityish. There is some context magic going on. Or doesn't this solve your problem? Luke
Roles and Trust
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 This would get rid of all of the bad UNIVERSAL::isa type of code and allow Perl to handle the dispatching instead of the programmer. While most probably don't do stuff like this, I think it's a great example of why role should be allowed to specify trust. I've been using "role-like" behavior quite a bit in a large test suite I'm working on and it would be nice to be able to do stuff like this. Cheers, Ovid -- If this message is a response to a question on a mailing list, please send follow up questions to the list. Web Programming with Perl -- http://users.easystreet.com/ovid/cgi_course/