Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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

by legato (Monk)
on Jan 19, 2005 at 16:05 UTC ( #423422=note: print w/ replies, xml ) Need Help??


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

I read through the overload docs, and while I understand bascially what it does, I can't seem to grok how to use overload to deal with this case. If you could provide a short example block of code, it would be most helpful to me.

Anima Legato
.oO all things connect through the motion of the mind


Comment on Re^2: Handling non-fatal method error strings without exceptions or globals?
Re^3: Handling non-fatal method error strings without exceptions or globals?
by dragonchild (Archbishop) on Jan 19, 2005 at 16:22 UTC
    # 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.

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

Re^3: Handling non-fatal method error strings without exceptions or globals?
by Tanktalus (Canon) on Jan 19, 2005 at 16:24 UTC

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://423422]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (21)
As of 2014-10-30 15:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (208 votes), past polls