dmitri has asked for the wisdom of the Perl Monks concerning the following question:

I have a server written in Perl that does some tasks that clients ask it to. Requests and responses are passed using soap, namely SOAP::Lite. Responses can be successful or unsuccessful. The latter are of two types: error codes plus honest-to-God exceptions (well, as close as Perl comes to them, anyway) of type Error or Exception::Class. The problem occurs when SOAP::Serializer tries to serialize objects of type Error. Error.pm has the following code (shortened for our purposes):
use overload ("0+" => 'value'); sub value { my $self = shift; exists $self->{'-value'} ? $self->{'-value'} : undef; }
Note that this subroutine may return undef. Now, SOAP::Serializer calls subroutine gen_id on references, which for normal objects is just the value of memory address:
sub gen_id { sprintf "%U", $_[1] }
Now, the problem is that when objects of type Error do not have a value (returns undef), this produces a segfault in perl 5.6.1. This problem can be demonstrated with the following short script:
package My; use overload ('0+' => 'num_conv'); sub new { my $self = shift; bless {}, ref($self) || $self; } sub num_conv { undef } package main; my $var = My->new; print int($var);
We see that when numeric conversion overloading function returns undef to built-in Perl functions that invoke it, that crashes perl. My question is, how do I approach this problem? Do I try to Your input is greatly appreciated. Thank you.

Replies are listed 'Best First'.
Re: When numeric conversion overloading goes awry.
by dragonchild (Archbishop) on Jan 24, 2006 at 19:34 UTC
    Submit the following patch to the maintainer of Error.pm:
    -exists $self->{'-value'} ? $self->{'-value'} : undef; +exists $self->{'-value'} ? $self->{'-value'} : 0;
    The bug is that 0+ should always return something that is a number. Undef, as you've discovered, isn't a number.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
      That will technically work. Semantically, however, this may lead to problems -- I think SOAP::Serializer relies on this technique to generate unique IDs for objects. If there are two such Error objects, SOAP::Serializer may get confused. I will file a bug in Error.pm's CPAN page, however, as yours is a valid point.
        Then that's a bug in Soap::Serializer. UIDs are a well-studied space with UUID and Data::UUID simply being two modules I've personally used. That should be filed, as well.

        My criteria for good software:
        1. Does it work?
        2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: When numeric conversion overloading goes awry.
by chromatic (Archbishop) on Jan 24, 2006 at 23:03 UTC
      Oh, this is interesting. I will try this out...
Re: When numeric conversion overloading goes awry.
by Anonymous Monk on Jan 24, 2006 at 19:48 UTC