Data::Dumper and UTF-8

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I'm trying to make Dumper display an UTF-8 string but I can't get it to

$ cat
#!/usr/bin/perl -w

use strict;
use Data::Dumper;
use utf8;

binmode(STDOUT, ":utf8");

my $s = "\x";

print "$s\n";
print Dumper($s);
print Dumper("☺");

$ ./

$VAR1 = "\x";
$VAR1 = "\x";

Any clues?



Re: Data::Dumper and UTF-8

Quoted text here. Click to load it
Quoted text here. Click to load it
Quoted text here. Click to load it

   According to "perldoc Data::Dumper", this module is used for
stringifying perl data structures, suitable for both printing and

   I take that to mean that Dumper()'s output text will be portable
enough to be read in correctly by Data::Dumper on any platform, which
may mean that all the output text will be in straight ASCII (and not
in some flavor of Unicode).

   If you "eval" the output string, then the "eval"'s output should be
"=E2=98=BA".  Try adding this line at the end of your script to see what I

      print eval("my " . Dumper($s));  # prints 98=BA

(The "my " part is to suppress a warning given because the output's
"$VAR1" is caught by "use warnings;" and "use strict;".  Optionally,
you may remove that part by using the following lines instead:

      # Remove the "$VAR1 = " part with substr():
      print eval( substr(Dumper($s), 8) );

Either way should work.)

   So basically, Data::Dumper is outputting exactly what it should: a
string that, if "eval"ed, returns the contents of its input (which, in
your case, is $s).  That way, any platform can read it in, regardless
of whether its terminal window can support the ':utf8' output mode.

   I hope this helps, August.

   -- Jean-Luc

Re: Data::Dumper and UTF-8

Quoted text here. Click to load it

   Jason, I just now figured out (by reading "perldoc Data::Dumper")
that the "$VAR = " part can be suppressed by setting
$Data::Dumper::Terse to 1.  Therefore, you could add the following two
lines to the end of your script:

   $Data::Dumper::Terse = 1;  # to suppress "$VAR1 = "
   print eval Dumper($s);  # prints98=BA

and you'll see that, although Dumper may not output text in the form
you want, eval()ling the output text does return it in the form you

   -- Jean-Luc

Site Timeline