Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Just off the top of my head, I would do something like this:

package Result; use overload 'bool' => \&as_bool, '""' => \&stringify; sub new { if ($_[0] eq __PACKAGE__) { shift; } my $self = { as_bool => $_[0], string => $_[1], }; bless $self, __PACKAGE__; $self; } sub as_bool { my $self = shift; $self->{as_bool}; } sub stringify { my $self = shift; $self->{string}; } 1;

Then you can use it like this:

require Result; sub complex_function { # ... if ($error_message) { return Result->new(undef, $error_message); } return Result->new(1); }

With this, the caller wouldn't change much at all:

unless (my $r = complex_function()) { print "Error received: $r\n"; }

Hope that helps. (Completely untested ;->) Also, the way it is now, Result is not a class you can easily derive from ... I leave that as an excersise for other monks ;->


In reply to Re^3: Handling non-fatal method error strings without exceptions or globals? by Tanktalus
in thread Handling non-fatal method error strings without exceptions or globals? by legato

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-04-19 09:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found