Data::Dumper and UTF-8

Data::Dumper and UTF-8

am 21.10.2007 23:27:07 von August Karlstrom

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

Re: Data::Dumper and UTF-8

am 21.10.2007 23:32:34 von vbMark

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

Re: Data::Dumper and UTF-8

am 22.10.2007 09:43:42 von augukarl

On 21 Okt, 23:32, Brian Wakem wrote:
> What version of Perl?

v5.8.8

> Fromhttp://search.cpan.org/~ilyam/Data-Dumper-2.121/Dumper.p m
>
> "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

Re: Data::Dumper and UTF-8

am 22.10.2007 09:45:54 von Peter Makholm

augukarl@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

Re: Data::Dumper and UTF-8

am 22.10.2007 20:34:38 von jl_post

On Oct 21, 3:27 pm, August Karlstrom wrote:
>
> I'm trying to make Dumper display an UTF-8 string but I can't get it to
> work:
>
.
.
.
> print Dumper("â˜=BA");
.
.
.
> $VAR1 =3D "\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
"â˜=BA". Try adding this line at the end of your script to see what I
mean:

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

Re: Data::Dumper and UTF-8

am 22.10.2007 23:35:42 von jl_post

On Oct 22, 12:34 pm, "jl_p...@hotmail.com"
wrote:
>
> print eval("my " . Dumper($s)); # prints â˜=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 â˜=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

Re: Data::Dumper and UTF-8

am 27.10.2007 15:42:19 von hjp-usenet2

On 2007-10-21 21:27, 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}";

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.
| | | hjp@hjp.at |
__/ | http://www.hjp.at/ | -- Sam in "Freefall"