Moose::Error::Croak error reporting not from perspective of caller.

Posted by crashpoint_zero on Stack Overflow See other posts from Stack Overflow or by crashpoint_zero
Published on 2010-05-02T07:53:20Z Indexed on 2010/05/02 7:57 UTC
Read the original article Hit count: 490

Filed under:
|

I just recently started out on Moose and its a great OO framework not only to use but also to learn new OO concepts. One of the things I wanted to do was to do error reporting from perspective of caller during object creation. I saw that Moose has the module Moose::Error::Croak which tells Moose to override the default error reporting by croak call. I used it but it did not seem to help

Moose code - Foo.pm

package Foo;
use metaclass (
  metaclass => 'Moose::Meta::Class',
  error_class => 'Moose::Error::Croak',
);
use Moose;
has 'attr1' => (
  is => 'rw',
  isa => 'Str',
  required => '1',
);
no Moose;
1;

Moose code - fooser.pl

#!/usr/bin/perl
use strict;
use warnings;
use Foo;
my $foobj = Foo->new();

This fails with error: Attribute (attr1) is required at /usr/local/lib/perl/5.8.8/Class/MOP/Class.pm line 364

which is terse than the actual stack trace if Moose::Error::Croak is not used. But it does not report it from perspective of caller. If this were a Perl 5 OO code and I had Foo.pm as:

package Foo;

use strict;
use warnings;
use Carp;

sub new { 
  my ($class, %args) = @_;
  my $self = {};
  if (! exists $args{'attr1'}) {
    croak "ERR: did not provide attr1";    
  }
  $self->{'attr1'} = $args{attr1};
  bless $self, $class;
  return $self;
}
1;

And if fooser.pl was executed I would have got the error:

"ERR: did not provide attr1 at fooser.pl line 6"

which is from the perspective of the caller as it points to line no. 6 of fooser.pl rather than MOP.pm's line no. 364.

How can I do this in Moose? Or am I misunderstanding something here?

© Stack Overflow or respective owner

Related posts about perl

Related posts about moose