Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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

by Lightknight (Sexton)
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?

Comment on Why does this combo of Data::Dumper and Clone::clone() fail? Bug?
Select or Download Code
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 a.pl 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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://877538]
Approved by superfrink
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2015-07-30 17:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (273 votes), past polls