leonerd / perl5

🐫 The Perl programming language
https://dev.perl.org/perl5/
Other
7 stars 1 forks source link

Data::Dumper doesn't support classes #18

Open wolfsage opened 2 years ago

wolfsage commented 2 years ago
class Hello {}
print Dumper(Hello->new);
cannot handle ref type 16 at lib/Data/Dumper.pm line 213.
$VAR1 = bless( , 'Hello' );
wolfsage commented 2 years ago

Should it dump fields? Would be useful but that's private data unless explicitly exposed? Hmm.

leonerd commented 2 years ago

Should it dump fields? I don't know. Data::Dumper is duty-bound to only print valid perl code that theoretically someone could eval() again to recreate a decent approximation to the original value that was passed in.

Example, in this case:

class HiddenField {
   field $x;  ADJUST { $x = int( rand * 256 ) }
}
my $obj = HiddenField->new;
Dumper($obj)

what valid perl code should be printed? What can you possibly eval() again to recreate that object?

This was touched on by https://github.com/Ovid/Cor/issues/63

leonerd commented 2 years ago

Alternative thought: Data::Dumper being duty-bound to print valid eval()-able Perl code, means it cannot and should never support this. We should train users out of expecting to use Data::Dumper just to get some human-readable debug printing, and instead supply them with a better (core-supported) mechanism that doesn't have that restriction. Something like Data::Printer perhaps.