Chris Prather also posted this excellent example on use.perl based on
something Dylan Hardison said recently.
role Pet::Sounds {
method make_noise { say $self->noise }
}
class MyHouse {
has pet => (
does => 'Pet::Sounds',
required => 1,
handles => 'Pet::Sounds',
);
}
MyHouse->new(pet => Beetle->new())->make_noise; # I want pet provided
on creation but never accessed directly after that.
In this example the attribute exists only to delegate methods and does
not need an accessor.
- Stevan
On Jun 3, 2009, at 1:06 PM, Mark Morgan wrote:
This behaviour has bitten me a few times too, taking quite a bit of
time to find the cause of. Is there any intention to give a default
'is' value, or raise a warning, if no access parameters are given?
Mark.
On Wed, Jun 3, 2009 at 3:09 PM, mikhail maluyk <[email protected]
> wrote:
Hi,
"has" doesn't create an accessor for an attribute. "is" or
"accessor" does.
On Wed, Jun 3, 2009 at 8:00 PM, Ovid <publiustemp-
[email protected]> wrote:
Could someone please explain to me what I'm doing wrong here?
#!/usr/bin/env perl
{
package My::Base;
use Moose;
has some_method => ( default => 'foo' );
}
{
package My::Class;
use Moose;
extends 'My::Base';
}
print My::Base->new->some_method;
__END__
Can't locate object method "some_method" via package "My::Base"
at
inherit.pl line 16.
Changing the some_method attribute to the following makes this go
away:
has some_method => ( is => 'rw', default => 'foo' );
(is => 'ro', default => 'foo') also works.
Is this a bug or have I misunderstood something? This is Moose
0.79 and
perl, v5.8.8 built for i486-linux-gnu-thread-multi. (Also fails on
Solaris,
so I doubt it's an OS issue).
If this is a bug, I'll file a report.
Cheers,
Ovid
--
Buy the book - http://www.oreilly.com/catalog/perlhks/
Tech blog - http://use.perl.org/~Ovid/journal/
Twitter - http://twitter.com/OvidPerl
Official Perl 6 Wiki - http://www.perlfoundation.org/perl6
--
Regards,
Mikhail