My module throws warnings and errors - it generates both by printing to STDERR

No, it does not, at least not in version 1.11 as published on CPAN. It throws no errors and no warnings. It just prints to STDERR, although it looks like it should alternatively call a custom function. (See below the line.)

To throw errors, call die. To throw warnings, call warn. To suppress "at <filename> line <number>", append a newline to the message. To get a stack trace, see Carp.


I think this does not do what you want:

# Returns error if last operation failed sub error { my $self = shift; return $self->{'error'}; } # ... sub _error { my ($self, $message) = @_; $self->{'error'} = $message; # <--(1) if (defined &{$self->{'error'}}) { # <--(2) &{$self->{'error'}}($message); # <--(3) } else { STDERR->print("Stripe Webhook Error: $message\n"); } }

At (1), you store the error message in $self->{'error'}, much like you do in sub error. At (2), you check if $self->{'error'} that you just have overwritten contains an error callback function which you try to call at (3). Unless $message is set to a valid function name, this code will always print to STDERR.

I think what you wanted to implement were two attributes, one for the error message (e.g. $self->{'errorMessage'}) and one for an error handler (e.g. $self->{'errorHandler'}).

But what you really want is just a proper exception. Don't re-implement the wheel, don't mess up your object with all of that error handling nonsense. If something goes wrong, just call die and leave it to the user if (s)he wants to use a plain old eval, something like Try::Tiny, the fancy new try-catch-finally, or just let the program die. Same for warnings. Just call warn and let the user decide if warnings should cause some action or should be ignored.

If you want fancy stack traces, use Carp instead of plain die/warn.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

In reply to Re^5: STDERR in Test Results by afoken
in thread STDERR in Test Results by Bod

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.