pg has asked for the wisdom of the Perl Monks concerning the following question:

I was working on something, and then this 'small trick' made me stuck for couple of minutes. I just don't want everyone to waste this couple of minutes, which is so precious, so I come to share with you.

The problem is with Data::Dumper, it appears to be a little bit too smart. Because my program is relatively big, and what I was modifing was hundreds lines away from Dumper, so at first I even didn't think that, the trouble was caused by Dumper, until I suddenly started to suspect ... Now I simplify the trick and share with you:
use Data::Dumper; $hash = {1 => "(\\d+)"}; #the value is (\d+) print "result from print Dumper:\n"; print Dumper($hash); #which prints (\\d+), this really confused me print "\n"; print "actual value stored:\n"; print $hash->{1};
I hope Dumper can simply tell me the truth, not to play smart, and confuse me. Although that \\ makes sense to me now, but what is the benefit to display in this way?

Now, is there an option in Dumper, that I can turn this off.

Replies are listed 'Best First'.
•Re: Data::Dumper style
by merlyn (Sage) on Dec 08, 2002 at 04:40 UTC
      Im afraid to say that while I somewhat agree with you first statement, the second statement is bad advice. As far as broken dumpers go YAML is one of the worst. Data::Dumper is far more accurate in my testing. One of these days I will get around to publishing my Dumper comparison suite to back these words up. Suffice it to say that YAML will get your Data::Dumper bugreport wrong as well as number of much more simple examples. Some of these weaknesses are even listed in the YAML documentation.

      Unfortunately but IMO the current version of YAML is not suitable for much more than config files.

      --- demerphq
      my friends call me, usually because I'm late....

Re: Data::Dumper style
by demerphq (Chancellor) on Dec 08, 2002 at 10:22 UTC
    Hi pg,

    Im afraid you seem to have made an invalid assumption here:

    print Dumper($hash); #which prints (\\d+), this really confused me

    Dumper doesnt print (\\d+) it prints '(\\d+)' which is perfectly correct. Dumpers output (with any settings but Terse) is as merlyn pointed out meant to be evalled. This means that it emits valid perl statements to reconstruct the original data. So when it outputs a single quoted string it of course emits \\ instead of \ just as you used \\ inside of the doublequoted string when you assigned the value in the first place.

    Short of emitting HERE docs as part of its output (which would be a PIA to read IMO) there is no way for it to specify a \ inside of a quoted construct except as \\. (Well, thats not strictly true. Dumper could be smarter about knowing which \ sequnces actually need to be doubled, but IMO the output would look even stranger, with some \ doubled and some not.)

    The moral of the story is that Dumper does indeed get stuff wrong, but those things are rare indeed, and most of them are well known to the author and maintainers and to most of the more experienced members of the Perl community. It is unlikely that you will encounter a Dumper bug that isnt on this list. And incidentlly for the few things that do remain, they havent been fixed becuase currently it doesnt look like they are worth being fixed.

    #are these the same or different? my ($x,$y); $x=\$y; $y=\$x; print Dumper([$x,$y]); my $ar=[]; $ar->[0]=\$ar->[1]; $ar->[1]=\$ar->[0]; print Dumper($ar);
    HTH

    --- demerphq
    my friends call me, usually because I'm late....

Re: Data::Dumper style
by tadman (Prior) on Dec 08, 2002 at 04:37 UTC
    I think you're confusing "smart" with "working" since it's expected that you can just eval the code that Data::Dumper puts out. If you really feel burned by it, why not substitute? There's nothing to say you couldn't do this:
    package My::Dumper; use base 'Data::Dumper'; sub Dumper { my $result = Data::Dumper::Dumper(@_); $result =~ s/\\\\/\\/g; }