develooper Front page | perl.perl5.porters | Postings from October 2003

RE: Data::Dumper, BigInt, GMP and Dumper()

Thread Previous | Thread Next
From:
Orton, Yves
Date:
October 13, 2003 02:54
Subject:
RE: Data::Dumper, BigInt, GMP and Dumper()
Message ID:
71B318898201D311845C0008C75DAD1C089612DA@defra1ex2
> v5.8.1 does Dump Math::BigInt variables wrong when their 
> internal data typ is 
> a Math::BigInt::GMP:
> 
> 	te@null:~> perl -MMath::BigInt=lib,GMP -MData::Dumper -le '$a = 
> 	Math::BigInt->new(123); print Dumper( $a )'
> 	$VAR1 = bless( {
>                  'sign' => '+',
>                  'value' => bless( do{\(my $o = 137245040)}, 
> 		'Math::BigInt::GMP' )
>                }, 'Math::BigInt' );

Could you tell me what this _should_ look like please? I am working a new
dumper to resolve certain problems in Data::Dumper (Its mostly complete now)
and I will endeavour to ensure that Math::BigInt objects are dumped
correctly. 

 
> Is there any way to specifiy how an object (c/sh)ould be 
> dumped? Like adding a 
> DUMPME routine that returns the string for Dumper()? Hm, the doc says 
> Data::Dumper::Freezer might be usefull. Let's try it:
> 
> 	te@null:~> perl -MMath::BigInt=lib,GMP -MData::Dumper -le '$a =
> 	Math::BigInt->new('12345678901234567890'); 
> $Data::Dumper::Freezer = 'bstr';
> 	print Dumper( $a )
> 	Segmentation fault
>
> Oops. Maybe it wants a coderef despite the doc claiming otherwise?

No it doesnt want a coderef. Freezer is a subroutine that needs to change
the INTERNAL state of the object. 'bstr' if I understand correctly returns a
string. This is not the same thing as collapsing the internals of the object
into a format that can be dumped.

Think of it this way. Freezer tells the object to pack itself up into a
portable format. Toaster tells the object that it is in a portable format
and that it should now unpack itself into a real object for use.

So given what ive seen of the dump above you might want something like this:

sub Math::BigInt::Freezer {
  my $self=shift;
  my $bstr=$self->bstr();
  %$self=(FROZEN=>$bstr);
}

sub Math::BigInt::Toaster {
  my $self=shift;
  # ...  contents left as an excercise for the student :-) ... 
}

So now you would say

$Data::Dumper::Freezer="Freezer";
$Data::Dumper::Toaster="Toaster";

And things should work

 
> No. Maybe let's try 'print'. Just for kickers:
> 
> 	te@null:~> perl -MMath::BigInt=lib,GMP -MData::Dumper -le '$a =
> 	Math::BigInt->new('12345678901234567890'); 
> $Data::Dumper::Freezer = 'print';
> 	print Dumper( $a )'
> 	WARNING(Freezer method call failed): Can't locate 
> object method "1" via

I find this interesting. I dont see why it happened like that at all. It
should say
cant find method 'print' via Math::BigInt...

BTW, the perl code for freezing in DD is really simple:

    if (my $freezer = $s->{freezer}) {
      $val->$freezer() if UNIVERSAL::can($val, $freezer);
    }

The weird bugreports youve recieved are beyond me as I dont understand the
DD XS code that well. Try using the latest DD and set
$Data::Dumper::Useperl=1 to force it to use the perl code only. (This also
relieves some memory leaks encountered in the XS code on Win32.)

So, what you _might_ want to do is define a freezer/toaster method for
Math::BigInt in general.

This would be useful becuase _my_ dumper (Data::BFDump v0.6, not the version
on CPAN), handles per class Freezer methods (instead of DD's one per all
classes being dumped), and given that Math::BigInt is part of core I would
have no problem preseeding the table with the appropriate methods.

HTH,
Yves



Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About