Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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

by dragonchild (Archbishop)
on Jan 19, 2005 at 15:15 UTC ( #423406=note: print w/ replies, xml ) Need Help??


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

What's wrong with $object->get_error? This way you can have object-specific errors.

Obviously (as I confirmed in a CB session with tye,theorbtwo and others) I cannot return error strings that are logically false, but I'd like to preserve the illusion.

That's not entirely true. You can use overload to create an object that will return a string when stringified and false when boolified. And, frankly, that's what I'd do if you're not going to allow exceptions or globals, but still require an error string.

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.


Comment on Re: Handling non-fatal method error strings without exceptions or globals?
Download Code
Re^2: Handling non-fatal method error strings without exceptions or globals?
by legato (Monk) on Jan 19, 2005 at 16:05 UTC

    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

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

      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://423406]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2014-07-12 18:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (240 votes), past polls