Click here to get back home

Data::Dumper and UTF-8

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Data::Dumper and UTF-8 August Karlstrom 10-21-2007
Get Chitika Premium
Posted by August Karlstrom on October 21, 2007, 5:27 pm
Please log in for more thread options
Hi,

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

$ cat test.pl
#!/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("☺");

$ ./test.pl

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

Any clues?


Regards,

August

Posted by jl_post@hotmail.com on October 22, 2007, 2:34 pm
Please log in for more thread options
>
> I'm trying to make Dumper display an UTF-8 string but I can't get it to
> work:
>
.
.
.
> print Dumper("=E2=98=BA");
.
.
.
> $VAR1 =3D "\x";
>
> Any clues?


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

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
mean:

print eval("my " . Dumper($s)); # prints =E2=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 =3D " 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


Posted by jl_post@hotmail.com on October 22, 2007, 5:35 pm
Please log in for more thread options
wrote:
>
> print eval("my " . Dumper($s)); # prints =E2=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 =3D " part with substr():
> print eval( substr(Dumper($s), 8) );
>
> Either way should work.)


Jason, I just now figured out (by reading "perldoc Data::Dumper")
that the "$VAR =3D " 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 =3D 1; # to suppress "$VAR1 =3D "
print eval Dumper($s); # prints =E2=98=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
want.

-- Jean-Luc


Similar ThreadsPosted
eval of Data::Dumper output not same as original data structure August 1, 2007, 3:05 am
Re: Data::Dumper and UTF-8 October 21, 2007, 5:32 pm
Re: Data::Dumper and UTF-8 October 27, 2007, 9:42 am
Installation of Data::Dumper June 10, 2005, 2:41 am
Re: Installation of Data::Dumper August 29, 2005, 3:44 am
Data::Dumper not indenting per perldocs January 29, 2007, 9:35 pm
Data::Dumper displaying empty arrays / hashes January 30, 2006, 5:23 am
How can i find source code like Data::Dumper in perl package October 20, 2006, 10:21 am
XML-RPC calls with real UTF8 data failed July 20, 2006, 11:40 am
Decode data of different charsets into UTF8 (Perl internal format) September 8, 2006, 8:36 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap