http://qs321.pair.com?node_id=423425


in reply to Re^2: Handling non-fatal method error strings without exceptions or globals?
in thread Handling non-fatal method error strings without exceptions or globals?

# This code has been run through a basic smoke-test. package My::Error::String; use overload '""' => \&stringify, '0+' => \&boolify, fallback => 1; sub new { my $class = shift; my $error = shift; bless \$error, $class; } sub stringify { my $self = shift; return $$self; } sub boolify { return; }

Then, when you want to use it, you'd do something like:

use My::Error::String; sub func_with_error { if ($have_error) { return My::Error::String->new( "Have an error somewhere ..." ) +; } }

Being right, does not endow the right to be rude; politeness costs nothing.
Being unknowing, is not the same as being stupid.
Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Replies are listed 'Best First'.
Re^4: Handling non-fatal method error strings without exceptions or globals?
by Tanktalus (Canon) on Jan 19, 2005 at 16:34 UTC

    Note that if you don't have an error, this code is assuming you will not return an object. (Just want to make clear the reason why boolify always returns false.)