No More Moose Loose Ends.

Final final clean up day in the Moose-Pen

Just a few quick things here in the Moose-Pen today. I want to make sure that my '_one_error' sub will return the same style of results no matter from where it is called and I want to make sure the 'DA_ALL_ERRORS' $ENV var is respected.

I did the $ENV first at that was a little tickly; I made this change in Accessor.pm


if ($@) {
-- if (exists($ENV{'DA_ALL_ERRORS'}) and $ENV{'DA_ALL_ERRORS'} ){
-- die $@;
-- }
-- else {
-- _one_error($ops,'new',$package, $filename, $line, $subroutine);
++ _one_error($@, $ops,'new',$package, $filename, $line, $subroutine);
-- }
}

To drop the actual error into my '_one_error' sub. Now in the sub I had to account for that like this

sub _one_error {
my ( $error_in, $ops, $call, $package, $filename, $line, $subroutine ) = @_;
my @errors;
my $error_msg;
my $error;

if ( exists( $ENV{'DA_ALL_ERRORS'} ) and $ENV{'DA_ALL_ERRORS'} ) {
die $error_in;
}
else {


Here the really only important thing to watch out for is you can't reuse that '$@' var in that sub so I renamed it $in_error and replaced it where necessary and then used the same if from the code I took out of Accessor.pm and just simply die with it if the $ENV var is present.

Now for types I ended up with


coerce 'Predicate', from 'HashRef',
via {
my $object;
if($NEW){
$object = Database::Accessor::Predicate->new( %{$_} ) ;
}
else {

eval {
$object = Database::Accessor::Predicate->new( %{$_} ) ;
};
if ($@) {

my ($package, $filename, $line, $subroutine) = caller(11);
$subroutine =~ s/Database\:\:Accessor\:\://;
_one_error($@,$_,$subroutine,$package, $filename, $line, $subroutine);
}

}
return $object;
};

Here the only neat thing is that I use the caller function right out to '11' to get the correct sub that is being called. In this case I have to strip out the class name so it looks nice on my error message;

Database::Accessor add_condition Error:
The following Attribute is required: (conditions->left)
With constructor hash:
{
'operator' => '=',
'right' => {
'value' => 'test'
},
'leftx' => {
'view' => 'People',
'name' => 'last_name'
},
'close_parentheses' => 0,
'condition' => 'AND',
'open_parentheses' => 1
}
at 43_dynamic_conditions.t line 99

Great. The last thing for today is another re factoring exercise. I really do not need to redo the if ($NEW) and eval over and over so the above is now;

coerce 'Predicate', from 'HashRef',
via {
my $object = _create_instance($_,'Database::Accessor::Predicate');
return $object;
};

sub _create_instance{
my ($ops,$class) = @_;
my $object;
if($NEW){
$object = $class->new( %{$ops} ) ;
}
else {

eval {
$object = $class->new( %{$ops} ) ;
};
if ($@) {

my ($package, $filename, $line, $subroutine) = caller(12);
$subroutine =~ s/Database\:\:Accessor\:\://;
_one_error($@,$ops,$subroutine,$package, $filename, $line, $subroutine);
}

}
return $object;
}

The only change here is now I have to use caller(12) as I am one more sub deeper. Now I can reuse that sub in the other coerce statements in my Types class.

DEn0eAXUMAAB-Cg.jpg

Leave a comment

About byterock

user-pic Long time Perl guy, a few CPAN mods allot of work on DBD::Oracle and a few YAPC presentations