[Dbix-class] How can the cookbook Data::Dumper recipe possibly work?

Peter Valdemar Mørch peter at morch.com
Wed Jul 20 03:32:36 GMT 2011


Hi,

I'm having trouble getting this cookbook recipe to work:

  package My::DB::CD;

  sub _dumper_hook {
    $_[0] = bless {
      %{ $_[0] },
      result_source => undef,
    }, ref($_[0]);
  }

  [...]

  use Data::Dumper;

  local $Data::Dumper::Freezer = '_dumper_hook';

  my $cd = $schema->resultset('CD')->find(1);
  print Dumper($cd);
         # dumps $cd without its ResultSource

How is modifying $_[0] going to do anything for the caller? Instead this:

  sub _dumper_hook {
    $_[0]->{result_source} = undef;
  }

Does work, but smashes the original object, so that this:

   print Dumper($node);
   useNode($node);

makes useNode fail later.

Here is a snippet that illustrates:

package Foo;
use Data::Dumper;

sub freezer {
    my ($obj) = @_;
    $obj->{b} = 'newval';
    # Data::Dumper doesn't use any return value
    return bless { a => 'new', b => 'value' }, 'Foo';
}

local $Data::Dumper::Freezer = 'freezer';

my $object = bless { a=> 'a', b => 'b' }, 'Foo';

print Dumper($object);
print $object->{b}, "\n";

This prints out:

$VAR1 = bless( {
                 'a' => 'a',
                 'b' => 'newval'
               }, 'Foo' );
newval

$Data::Dumper::VERSION eq '2.131'

I'd love to be able to print Dumper($object); and then be able to use
$object later, but I think that isn't possible...  Instead, I created
a sub dumpCopy so that I can print Dumper($object->dumpCopy()) Am I
missing something?

Peter
--
Peter Valdemar Mørch
http://www.morch.com



More information about the DBIx-Class mailing list