How about a 'global' errors hash. Here is a little module that i used on an HTML::Template application, particularly, did the user correctly fill out the form:
package Error; use HTML::Template; use strict; sub new { my ($class,$file) = @_; my $self = { ERRORS => undef, # this key is a list of hashes file => $file, }; bless $self, $class; return $self; } sub add_error { my ($self,$field,$reason) = @_; push @{$self->{ERRORS}}, { FIELD => $field, REASON => $reason, }; } sub generate_errors { my ($self,$file) = @_; return unless &not_empty; my $template = HTML::Template->new(filename => $self->{file}); $template->param( IS_SINGULAR => (scalar @{$self->{ERRORS}} == 1), ERRORS => $self->{ERRORS}, ); @{$self->{ERRORS}} = (); return $template->output; } sub not_empty { my $self = shift; return ($self->{ERRORS}) ? (scalar @{$self->{ERRORS}} > 0) : 0; } 1;
And a client uses it like so:
use Errors; my $ERROR = Error->new('error.tmpl'); # etc. . . . sub validate_offer_name { my $offer_name = strip_spaces($CGI->param('offer_name')); unless ($offer_name =~ m/^[A-Za-z]\w*$/) { # ERROR - user entered invalid offer name $ERROR->add_error('Create New Offer', 'Invalid characters were + found'); go_home(); return; } if (&check_existing($DBH,$offer_name)) { # ERROR - name already exists $ERROR->add_error('Create New Offer', 'That name exists, try a +gain'); go_home(); return; } # success - valid name that does not exist print_create_offer_form($offer_name); } sub go_home { # if there are no errors, $errors will be undefined my $errors = $ERROR->generate_errors; my $template = HTML::Template->new(filename => 'home.tmpl'); $template->param( EXISTING_OFFERS => fetch_all_offers($DBH), EXISTING_TRANSPORTS => fetch_all_transports($DBH), ERRORS => $errors, ); print $CGI->header; print $template->output; }
The error template is included by all other templates and looks like this:
The following <TMPL_IF NAME="IS_SINGULAR"> error was <TMPL_ELSE> errors were <TMPL_IF> found: <TABLE WIDTH="95%"> <TMPL_LOOP NAME=ERRORS> <TR> <TD CLASS=body WIDTH="20%" ALIGN=right> <FONT COLOR=red><TMPL_VAR NAME=FIELD></FONT>: </TD> <TD CLASS=body ALIGN=left> <TMPL_VAR NAME=REASON> </TD> </TR> </TMPL_LOOP> </TABLE> Please try again
That's a lot of code to chew on - but i wanted to give you something that actually worked. The idea is to use a class to collect all the errors. After all possible user errors have been tested, any failures will be stored in the Error object. Then, you just tell the object to generate it's errors - if there are none, nothing happens, otherwise, in this case, the errors are reported back to the user along with the form that they failed to fill out correctly.

If using a full-blown object is too much for your needs, just boil the code down to it's basic element - a hash. As you find errors, just shove them into a hash (or an array, but a hash is more flexible), when the time comes to see if any errors were generated - do it once, not each time you check a value.

Tear that apart and build something useful for yourself :)

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--

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.