Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Data::Dumper and UTF-8

2 views
Skip to first unread message

August Karlstrom

unread,
Oct 21, 2007, 5:27:07 PM10/21/07
to
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{263a}";

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

$ ./test.pl

$VAR1 = "\x{263a}";
$VAR1 = "\x{263a}";

Any clues?


Regards,

August

Brian Wakem

unread,
Oct 21, 2007, 5:32:34 PM10/21/07
to
August Karlstrom wrote:

> 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{263a}";
>
> print "$s\n";
> print Dumper($s);

> print Dumper("?");
>
> $ ./test.pl
> ?


> $VAR1 = "\x{263a}";
> $VAR1 = "\x{263a}";
>
> Any clues?


What version of Perl?

From http://search.cpan.org/~ilyam/Data-Dumper-2.121/Dumper.pm

"Pure Perl version of Data::Dumper escapes UTF-8 strings correctly only in
Perl 5.8.0 and later."


--
Brian Wakem

augu...@yahoo.se

unread,
Oct 22, 2007, 3:43:42 AM10/22/07
to
On 21 Okt, 23:32, Brian Wakem <n...@email.com> wrote:
> What version of Perl?

v5.8.8

> Fromhttp://search.cpan.org/~ilyam/Data-Dumper-2.121/Dumper.pm


>
> "Pure Perl version of Data::Dumper escapes UTF-8 strings correctly only in
> Perl 5.8.0 and later."

Yes, I've read that. What does Pure Perl mean?


August

Peter Makholm

unread,
Oct 22, 2007, 3:45:54 AM10/22/07
to
augu...@yahoo.se writes:

>> "Pure Perl version of Data::Dumper escapes UTF-8 strings correctly only in
>> Perl 5.8.0 and later."
>
> Yes, I've read that. What does Pure Perl mean?

It means implementet only using Perl and not by implementing parts of
it in C (or any other language).

//Makholm

jl_...@hotmail.com

unread,
Oct 22, 2007, 2:34:38 PM10/22/07
to
On Oct 21, 3:27 pm, August Karlstrom <fusionf...@comhem.se> wrote:
>
> I'm trying to make Dumper display an UTF-8 string but I can't get it to
> work:
>
.
.
.
> print Dumper("☺");
.
.
.

> $VAR1 = "\x{263a}";
>
> 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
"☺". Try adding this line at the end of your script to see what I
mean:

print eval("my " . Dumper($s)); # prints ☺

(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

jl_...@hotmail.com

unread,
Oct 22, 2007, 5:35:42 PM10/22/07
to
On Oct 22, 12:34 pm, "jl_p...@hotmail.com" <jl_p...@hotmail.com>
wrote:

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


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); # prints ☺

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

Peter J. Holzer

unread,
Oct 27, 2007, 9:42:19 AM10/27/07
to

Looks ok to me. What output did you expect?

hp


--
_ | Peter J. Holzer | I know I'd be respectful of a pirate
|_|_) | Sysadmin WSR | with an emu on his shoulder.
| | | h...@hjp.at |
__/ | http://www.hjp.at/ | -- Sam in "Freefall"

0 new messages