On Thu Nov 27 22:14:17 2014, anonymous wrote:
Show quoted text> Segmentation fault
>
> Code to test:
>
> use common::sense;
> use Math::GMP;
> use Data::Dumper;
>
> $Data::Dumper::Terse=1;
> my $a = Math::GMP->new(1);
> my $s = Dumper $a;
> $a = 1;
> my $e = eval ($s);
> print $e;
I'm not surprised it failed - the code Data::Dumper produces is never going to be able to do the right thing, and what it promises to provide isn't possible in the general case without the cooperation of the classes you want to dump.
The Math::GMP object encapsulates an opaque structure acquired from libgmp, the Data::Dumper string is just wrapping the pointer to that structure without doing anything to make it valid. That's an unavoidable limitation of Data::Dumper, not a bug in Math::GMP.
I think you'd need to coerce Data::Dumper to represent a Math::GMP object not as "bless { some stuff }, 'Math::GMP'", but rather as 'Math::GMP->new("$object")' (ie, stringify the object to get the number in a form you can recreate the object).
You may be able to do this using Data::Dumper's Freezer and/or Toaster methods. Something like this appears to work, though I'm sure it could do with refinement:
$Data::Dumper::Freezer = 'Freezer';
$Data::Dumper::Toaster = 'Toaster';
{
package Math::GMP::as_string;
sub new {
my($class, $val) = @_;
my $str = "$val";
return bless \$str, $class;
}
sub Toaster {
my($obj) = @_;
return Math::GMP->new($$obj);
}
}
sub Math::GMP::Freezer {
my($obj) = @_;
$_[0] = Math::GMP::as_string->new($obj);
}
If you have other types of objects in your application you'll need to beware of interactions though, you may want to look whether an alternative like Data::Dump::Streamer will give you finer-grained control.
Hugo