in reply to Re: How to return errors from my object oriented subroutine
in thread How to return errors from my object oriented subroutine

Okay, that makes more sense. In that case I would define a set of constants (although I would use globals for them rather the constant pragma) and use those as return values indicating the result of the test. These would be things like $EMAIL_OK, $EMAIL_PROFANE, $EMAIL_DUPLICATE, etc. The calling class can check them against the return value and take appropriate action.

if ($email->check_validity() == $EMAIL_DUPLICATE) { send_error_page(message => 'that e-mail is already in use'); }

Replies are listed 'Best First'.
Re3: How to return errors from my object oriented subroutine
by dragonchild (Archbishop) on Aug 19, 2003 at 20:31 UTC
    I would actually have various tests to determine stuff.
    if ($email->is_duplicate) { ... } if ($email->is_profane) { ... } # etc ... package MyEmail; sub is_duplicate { my $self = shift; $self->validate unless $self->is_validated; return $self->{is_duplicate}; } # all the others are the same sub validate { my $self = shift; # set some attributes $self->{is_validated} = 1; }

    This way, you can check any of the possible error messages and the first time an error is checked, the file will be validated.

    ------
    We are the carpenters and bricklayers of the Information Age.

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: Re: Re: How to return errors from my object oriented subroutine
by princepawn (Parson) on Aug 19, 2003 at 20:21 UTC
    if ($email->check_validity() == $EMAIL_DUPLICATE) { send_error_page(message => 'that e-mail is already in use'); }
    Now here we have to be careful not to mix display and business logic... this might be fine for now, but lateron if we want different UI's to the same business logic, eg. FTP service instead of web service, the send_error_page would have to be less UI-specific.

    But the focus is on clarifying the API of the email validation function, and I think the example is illuminating for that purpose.

    Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality

    The Emacs Code Browser

      It's an intentionally dumb example to show that we are taking some kind of action based on the return value. In a real app I would never put an error string in-line like that.