in reply to (jeffa) Re: style for returning errors from subroutines
in thread style for returning errors from subroutines

I like the idea of putting the errors in a object, having the storage and the handling in one place, I will definitely have a closer look at your code.

However, a problem with this seems to be that if a package is going to share the same error standard, it would need have it's own instance of the error class. So either I have to check in two places to find my error code, OR I would have to pass my main routine's error object explicitly to each subroutine call, eg:

use Users; use Error; my $error = new Error; ($formatted_users = &format(&Users::get_users($filehandle)) or $error->handle or $Users::error->handle; ====================OR==================== use Users; use Error; my $error = new Error; ($formatted_users = &format(&Users::get_users($file_handle,$error))) or $error->handle;
which are both a bit messier than I'd like (although I've supplied a pretty messy example)

Replies are listed 'Best First'.
(jeffa) 3Re: style for returning errors from subroutines
by jeffa (Bishop) on Jun 15, 2001 at 00:28 UTC
    If your Users module was object oriented, you could simply pass it a reference to a single Error object. Here is how I would handle your example with the Error module:
    package Users; use strict; sub new { my ($class,$error) = @_; my $self = { ERRORS => $error, # and maybe even more . . . $fh? }; bless $self, $class; return $self; } sub get_users { my ($self,$fh) = shift; # rest of your sub # oops, an error happened $self->{ERRORS}->add_error('oops','i like vb'); } # rest of your Users module package main; use strict; use Users; use Error; my $ERROR = Error->new(); my $user = Users->new($ERROR); $user->foo(); print $ERROR->generate_errors(); # or maybe even my @stuff = $user->foo() or print $ERROR->generate_errors();
    Also, I really should have included this method below in the Error class. I think it's name explains it's function well enough:
    package Error; sub clear { my ($self) = @_; $self->{ERRORS} = undef; } # rest of Error module
    My original requirements didn't warrant the need for that method, but yours just might.

    Jeff

    R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
    L-L--L-L--L-L--L-L--L-L--L-L--L-L--