Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Why does this combo of Data::Dumper and Clone::clone() fail? Bug?

by Lightknight (Beadle)
on Dec 16, 2010 at 18:38 UTC ( #877538=perlquestion: print w/replies, xml ) Need Help??
Lightknight has asked for the wisdom of the Perl Monks concerning the following question:

I didn't expect these results of using Data::Dumper (with Useperl(1)) and Clone::clone together. This snippet:
#!/usr/bin/perl -w use strict; use Clone; use Data::Dumper; printf "Clone: %s Data::Dumper %s\n", $Clone::VERSION, $Data::Dumper::VERSION; my $orig = { # This could be arbitrariliy complicated/nested # - this suffices for a demo # of the probem 'array' => [ 1,2,3 ] }; # Put it throught Data::Dumper with Userperl(1); my $d = Data::Dumper->new([$orig]); $d->Useperl(1); $d->Dump(); # Make a clone - it should be identical, I guess my $clone = Clone::clone($orig); # See that the clone has a different structure in array # (length of $$clone{array} is 0) printf "orig: %d clone %d\n", scalar(@{$$orig{array}}), scalar(@{$$clone{array}});
Produces this output:
Clone: 0.29 Data::Dumper 2.121_14 orig: 3 clone 0
I would have expected
Clone: 0.29 Data::Dumper 2.121_14 orig: 3 clone 3
Since the orig and the clone should have been identical. Right? Is there a bug here? Where?

Replies are listed 'Best First'.
Re: Why does this combo of Data::Dumper and Clone::clone() fail? Bug?
by ikegami (Pope) on Dec 16, 2010 at 20:46 UTC

    In some version of Perl, Data::Dumper inadvertently adds "@" magic to the arrays it dumps.

    That magic has been supported since Clone 0.30. It was originally reported here.

    Simplest test case:

    #!/usr/bin/perl -w use strict; use Clone; use Test::More tests => 1; diag("Clone " . $Clone::VERSION); my $orig = [ 1,2,3 ]; my $r = \$#$orig; # Add "@" magic to @$orig. my $clone = Clone::clone($orig); is(0+@$clone, 0+@$orig); 1;
    1..1 # Clone 0.29 not ok 1 # Failed test at line 9. # got: '0' # expected: '3' # Looks like you failed 1 test of 1.
    1..1 # Clone 0.31 ok 1

    Upgrade Clone to 0.30 or higher.

Re: Why does this combo of Data::Dumper and Clone::clone() fail? Bug?
by BrowserUk (Pope) on Dec 16, 2010 at 18:56 UTC

    FWIW: I get your expected results. Albeit from slightly later versions of both modules:

    C:\test>junk11 Clone: 0.31 Data::Dumper 2.125 orig: 3 clone 3

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://877538]
Approved by superfrink
[erix]: aka *falling forward*
[Corion]: virtualsue: That sounds weird... I don't think we do fancy stuff like chunked encoding or something. Maybe your DNS is unlucky and gets the low machine.
[Corion]: But at least headers should be available

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2017-12-13 12:00 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (360 votes). Check out past polls.