Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Roles and Trust

0 views
Skip to first unread message

Ovid

unread,
Oct 5, 2005, 4:22:57 PM10/5/05
to perl6-l...@perl.org
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/

Luke Palmer

unread,
Oct 5, 2005, 5:06:58 PM10/5/05
to publiustemp-...@yahoo.com, perl6-l...@perl.org
On 10/5/05, Ovid <publiustemp-...@yahoo.com> 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

Ovid

unread,
Oct 5, 2005, 5:41:38 PM10/5/05
to perl6-l...@perl.org
--- Luke Palmer <lrpa...@gmail.com> 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?

Piers Cawley

unread,
Oct 10, 2005, 4:32:08 PM10/10/05
to publiustemp-...@yahoo.com, perl6-l...@perl.org
Ovid <publiustemp-...@yahoo.com> 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:) { *@self }

method _attributes($attrs) {
my @attributes = $attrs.attributes
return @attributes[0] if @attributes == 1;
...
}

Assuming it's legal.

--
Piers Cawley <pdca...@bofh.org.uk>
http://www.bofh.org.uk/

Ovid

unread,
Oct 12, 2005, 1:33:47 PM10/12/05
to perl6-l...@perl.org
--- Piers Cawley <pdca...@bofh.org.uk> wrote:
>
> How about:
>
> my method SCALAR::attributes($self:) { $$self }
> my method HASH::attributes(%self:) { %self.kv }
> my method ARRAY::attributes(@self:) { *@self }
>
> 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?

Piers Cawley

unread,
Oct 13, 2005, 2:20:22 AM10/13/05
to publiustemp-...@yahoo.com, perl6-l...@perl.org
Ovid <publiustemp-...@yahoo.com> writes:

> --- Piers Cawley <pdca...@bofh.org.uk> wrote:
>>
>> How about:
>>
>> my method SCALAR::attributes($self:) { $$self }
>> my method HASH::attributes(%self:) { %self.kv }
>> my method ARRAY::attributes(@self:) { *@self }
>>
>> 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: ) { *@array }

definitely is legal (though it does rather suffer from the end-weight
problem to my way of thinking).

0 new messages