Re: Dear Moose docs...
by chromatic (Archbishop) on Feb 10, 2013 at 04:13 UTC
|
how do you call the accessor methods?
Just as you did. The interesting question the documentation hasn't made clear to you is "How do I initialize attributes when I call the constructor?":
my $p = Point->new( x => 10, y => 20 );
The Objects chapter in the Modern Perl book may be a more gentle (and complete) introduction to Moose.
| [reply] [Watch: Dir/Any] [d/l] |
Re: Dear Moose docs...
by tobyink (Canon) on Feb 10, 2013 at 08:51 UTC
|
# Key-value pairs
my $point1 = Point->new(x => 10, y => 20);
# Hashref
my $point2 = Point->new({ x => 10, y => 20 });
In your class it's possible to override BUILDARGS to add your own argument parsing...
use v5.12;
{
package Point;
use Moose;
has 'x' => (is => 'rw', isa => 'Int');
has 'y' => (is => 'rw', isa => 'Int');
around BUILDARGS => sub {
my $orig = shift;
my $self = shift;
# Allow object to be built by passing two integers...
if (@_ == 2 and $_[0] =~ /^[0-9]+$/ and $_[1] =~ /^[0-9]+$/) {
return { x => $_[0], y => $_[1] };
}
# If not two integers, then fall back to standard Moose BUILDA
+RGS...
return $self->$orig(@_);
};
__PACKAGE__->meta->make_immutable;
}
# Use our overridden BUILDARGS...
print Point->new(10, 20)->dump;
# Standard Moose BUILDARGS is fallback...
print Point->new(x => 10, y => 20)->dump;
print Point->new({ x => 10, y => 20 })->dump;
package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Dear Moose docs...
by Your Mother (Archbishop) on Feb 10, 2013 at 05:10 UTC
|
use strict;
use warnings;
...
use Moose; # automatically turns on strict and warnings
Not to be snarky, just 'cause it was hanging down.
Moose doesn't aim to teach OO but to be a platform for it. Reading Conway's OO book or looking for general Perl OO tutorials would probably eliminate the lack of clarity, which I never noticed but can understand once someone else points it out.
In FOSS you're not gonna find much play in UR TEH SUCK. Better to say, I wanted to do X, I failed to find how in Y, if Z were there or you can point me to it, I'd love that!
| [reply] [Watch: Dir/Any] [d/l] |
|
Moose doesn't aim to teach OO indeed, but the OPs question falls under how-to use moose, not oo
| [reply] [Watch: Dir/Any] |
|
Oh, I know but the weakness in the docs was a complete surprise to me because I have the POOP background, heh-heh, heh-heh… To someone who has that context, the docs don’t need help at all. Fresh eyes were needed to expose it. Friendly eyes would be more likely to rope a sucker like me into Pod edits and pull requests.
| [reply] [Watch: Dir/Any] |
Re: Dear Moose docs...
by moritz (Cardinal) on Feb 10, 2013 at 09:16 UTC
|
Your rant would be much better placed in Moose's bug tracker, preferrably with a patch attached that improves the docs. Then those people who actually work on Moose would read it, not us "innocent bystanders".
And it would have the advantage of possibly having a positive effect.
| [reply] [Watch: Dir/Any] |
|
Your rant would be much better placed in Moose's bug tracker, preferrably with a patch attached that improves the docs. How would OP write a patch when OP didn't know the answer?
| [reply] [Watch: Dir/Any] |
|
> How would OP write a patch when OP didn't know the answer?
Asking here, but friendly and constructive and w/o ranting.
Free software depends on collaboration and not on a "I want my money back" attitude.
| [reply] [Watch: Dir/Any] |
|
The OP could have started reading the Moose test suite to find out, or any working example code in the wild that uses Moose.
But I don't always expect this level from involvement, which is why I wrote "prefferably", to make it clear it's not a requirement.
| [reply] [Watch: Dir/Any] |
Re: Dear Moose docs... (tired)
by Anonymous Monk on Feb 10, 2013 at 04:04 UTC
|
Are you people that out of touch?? Its not that I disagree with your viewpoint, but I do recognize, its time for a break :)
| [reply] [Watch: Dir/Any] |
Re: Dear Moose docs...
by sundialsvc4 (Abbot) on Feb 10, 2013 at 19:34 UTC
|
Why not put your editorial money where your mouth is, and offer a patch to the Perldocs? Create a document and submit it to the powers-that-be who are maintaining the package. I’m sure that they would love the help. Good writers are hard to find. The only ones who can make Perl better than it is are, “us folks.” You certainly won’t be the only person who shares your particular difficulty with the documents, but if you write from your point of view the material that you wish you could have read ... the next person who also thinks like you do, thanks to you, won’t have to repeat your negative experience.
| [reply] [Watch: Dir/Any] |
Re: Dear Moose docs...
by syphilis (Archbishop) on Feb 10, 2013 at 11:23 UTC
|
I ... ummmm ... actually work in the dairy industry.
Take it from me ... you should *all* acquire more Moo stocks !!
;-)
Cheers, Rob | [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] |
Re: Dear Moose docs...
by jmlynesjr (Deacon) on Feb 11, 2013 at 01:49 UTC
|
| [reply] [Watch: Dir/Any] |
Re: Dear Moose docs...
by 7stud (Deacon) on Feb 10, 2013 at 04:06 UTC
|
I'm still editing and trying to aquire the right tone. :( | [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] |