On Mon, Dec 01, 2003 at 10:42:09AM -0500, Hodges, Paul wrote:
> So is this a good time to divert this topic into an elaboration of roles?

I can wait for A12, but in the mean time ...  :-)

> So if I want to implement a package of related roles that I commonly use in
> our In-House code, such as to specify how verbose an object should be on log
> output, and how touchy it should be over whether a problem is screamingly
> fatal or just warningly annoying, and maybe toss in a shorthand method that
> lets me pass the names of some specific traits I want to be able to use (
> like "allow(qw/winkin blinkin nod/)" or some such), it should be pretty
> simple....but I'm having a hard time coming up with the syntax that declares
> the package and exports the roles. 
> 
>   module IHL::Roles;
>   @ISA = 'Exporter';
>   @EXPORT_OK = qw/ fatal verbose allow setvals /;
> 
>   our role fatal is property {
>       has $.fatal is rw;
>   }
> 
>   our role verbose is property {
>       has $.verbose is rw;
>   }
> 
>   sub allow {
>       .... # I'd want to make this one too complex to actually do it here
>   }
> 
>   sub import {
>       allow @_[1...];
>   }
> 

>   sub setvals ($o, [EMAIL PROTECTED]) {
>       "\$o.$_ = true".eval for @t; # surely there's a better way to do this?
>   }

Trying to keep up here ... but couldn't you do something like this?

        sub setvals ($o, [EMAIL PROTECTED]) {
           $o but= $_(true);
        }

er, wouldn't you *have* to do something like that.  If the object in
$o hasn't the property "foo", then C<$o.foo = true;> would be an
error, so you're trying to do 2 things: 1) give the object a property
and 2) assign a value to that property.

I think you're trying to do too much in the above code.  If you just
have a common set of roles that you want to use across objects, you
could do this:

        module MyRoles;
        use Exporter;
        @EXPORT_OK = <<verbose fatal>>;

        our property verbose is true;
        our property fatal is true;
        our property notexported;
        our property *trulyglobal;

then in other code:

        use MyRoles;
        use SomeFancyStuff;
        my $o = SomeFancyStuff.new() but verbose;
        $o but= MyRoles.notexported;
        $o but= trulyglobal;

Or am I missing something?

> then write allow() to build roles for each value passed in, maybe taking an
> arg to say whether they should be truly global, or built in the caller's
> namespace.... 

Isn't that what my, our, Exporter, and the globalifying * are all about?

I look forward with much anticipation to A12.

-Scott
-- 
Jonathan Scott Duff
[EMAIL PROTECTED]

Reply via email to