in reply to novice 'die' help requested
That changes the behavior, though. To get your original behavior, change warn to mywarn. I have no idea what mywarn() would do, because it seems like in your example code the string "invalid user" is never used for anything. So I'm unclear as to what it's for.sub check_id { my ( $id )= @_; my $res; eval { my $obj= ObjClass::Obj->new(); $obj->id( $id ); $obj->load_via_id() ; $res= $obj->id ; if (! $obj->activated_account) { DEBUG && warn "invalid user"; return -1; } } if ($@) { DEBUG && log_error( $@ ); return -1; } return $res; }
Slightly more generally, you could allow multiple of these eval blocks in a single routine with (excuse the funny indentation):
I haven't thought that through to what it would mean for chained blocks, partly because I'm not sure whether you mean nested blocks or a series of independent blocks.sub check_id { my ( $id )= @_; my $res; eval { { my $obj= ObjClass::Obj->new(); $obj->id( $id ); $obj->load_via_id() ; if (! $obj->activated_account) { warn "invalid user"; # or not... $res = -1; last; } $res= $obj->id ; } } if ($@) { DEBUG && log_error( $@ ); return -1; } return $res; }
Your return values seem a little odd, though. This routine can return three things: undef, -1, or a valid id, where -1 seems to mean either an expected or unexpected error and undef means... well, I don't know. There must be some reason why you're doing $res = $obj->id. Is there some reason why it might not return a valid id, other than the activated_account test failing? If not, then why not just set $res to -1 if activated_account fails? Why throw an exception at all if it isn't really an exceptional case? If there is some subset of cases where an undef return value is meaningful, then it seems like the caller of this function has to do too much work to decipher all the possible things the return value might mean.