-2

I an using strawberry perl, Moose 2.0010

In the class:

package Cat;
use 5.010;
use strict;
use Moose;

has 'name',       is => 'ro', isa => 'Str', default => 'Beauty';
#has 'age',       is => 'ro';
has 'diet',       is => 'rw', default => 'fish';
has 'birth_year', is => 'ro', isa=> 'Int',
                  default => 1997;

In application:

use 5.010;
use strict;
use Cat;

my $kitty = Cat->new(name => 123, diet => 'Sea food', 
                     birth_year => 'nineteen ninety seven');
say 'I have a kitten named ', $kitty->name(), ' eats ', $kitty->diet(),
    ' birth at ', $kitty->birth_year();

The output:

I have a kitten named 123 eats Sea food birth at nineteen ninety seven
Press any key to continue . . .

It doesn't force type checking.

Edit: The complete code, the rest of code is generated by Padre, I haven't delete it. Padre added trailing 1;:

package Cat;
use 5.010;
use strict;
use Moose;

has 'name',       is => 'ro', isa => 'Str', default => 'Beauty';
#has 'age',       is => 'ro';
has 'diet',       is => 'rw', default => 'fish';
has 'birth_year', is => 'ro', isa=> 'Int',
                  default => 1997;
sub age
{
    my $self = shift;
    my $year = (localtime)[5] + 1900;

    return $year - $self->birth_year();
}

=pod

=head1 NAME

Cat - My author was too lazy to write an abstract

=head1 SYNOPSIS

  my $object = Cat->new(
      foo  => 'bar',
      flag => 1,
  );

  $object->dummy;

=head1 DESCRIPTION

The author was too lazy to write a description.

=head1 METHODS

=cut

use 5.006;
use strict;
use warnings;

our $VERSION = '0.01';

=pod

=head2 new

  my $object = Cat->new(
      foo => 'bar',
  );

The C<new> constructor lets you create a new B<Cat> object.

So no big surprises there...

Returns a new B<Cat> or dies on error.

=cut

sub new {
    my $class = shift;
    my $self  = bless { @_ }, $class;
    return $self;
}

=pod

=head2 dummy

This method does something... apparently.

=cut

sub dummy {
    my $self = shift;

    # Do something here

    return 1;
}

1;

=pod

=head1 SUPPORT

No support is available

=head1 AUTHOR

Copyright 2011 Anonymous.

=cut
perigrin
  • 4,433
  • 19
  • 22
Weiyan
  • 1,116
  • 3
  • 14
  • 25
  • 2
    When I run this code I get ' Attribute (birth_year) does not pass the type constraint because: Validation failed for 'Int' with value "nineteen ninety seven" '. – perigrin Jun 22 '11 at 03:54
  • 1
    I ran the code as well, and I got an error just like perigrin's. Are you sure you're picking up the right module? Do you have an older, unmoosey package with the same interface? – unpythonic Jun 22 '11 at 04:00
  • Is that complete? Fails for me with `Cat.pm did not return a true value at kitty.pl line 3`!? `Cat.pm` should really end with the statement `1;` – dwarring Jun 22 '11 at 04:05
  • @snoopy: He asked a similar question using much the same code earlier. It too was missing the trailing `1;`. It too worked fine, so I don't think this is his actual code. For instance, I don't see anything in there which prompts: `Press any key to continue . . .` – unpythonic Jun 22 '11 at 04:18
  • Don't prejudge. Mark Mann is so rude to say I don't post actual code here. Should you say sorry to me? Could you show your evidence? The other similar question is it doesn't set the default value. I also surprised why it doesn't return error missing trailing 1;. I had change the "name" attribute to "ame", it returns "Can't locate object method.....". Which version of perl / platform are you using? – Weiyan Jun 22 '11 at 06:08
  • @Weiyan Mark Mann has a valid suspicion. Using 5.10.0 and copying your code *exactly* as you have posted: `Cat.pm did not return a true value `. This also happens on 5.14.0. When I fix this issue I get `Attribute (birth_year) does not pass the type constraint because: Validation failed for 'Int' with value nineteen ninety seven` which is the expected constraint validation. – perigrin Jun 22 '11 at 06:18
  • Suspicion is OK, please don't judge me that I don't post the actual code. I use Padre to start a new module, it hides a new method and the trailing 1; inside perl doc section. Clear out the codes generated by Padre, it works as expected. I agree I am careless in this case, but I never intend to not post actual code to treat all of you. – Weiyan Jun 22 '11 at 06:30
  • @Weiyan - I am sorry if I came across as rude. That was not my intent. That said, it becomes frustrating if you keep posting problems, asking for help from strangers, while not giving enough information for us to help you. – unpythonic Jun 22 '11 at 07:36

1 Answers1

2

The problem is the new defined at line 64 of Cat.pm. Moose provides a new method, so you don't need to write your own. Remove that new method, and it works fine.

The other bit that should have triggered a "aha!" is the

use 5.006;
use strict;
use warnings;

which is present halfway down your Cat.pm code.

If you're going to use POD documentation, you should either embed your code within the docs (allowing the docs to enhance your inline comments); or you should put all your code up top and make a clear POD at the bottom. Either way, consistency will help avoid these kinds of problems in the future.

unpythonic
  • 4,020
  • 19
  • 20