Data::Dumper and UTF-8

Discussion in 'Perl Misc' started by August Karlstrom, Oct 21, 2007.

  1. 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
    August Karlstrom, Oct 21, 2007
    #1
    1. Advertising

  2. August Karlstrom

    Brian Wakem Guest

    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
    Brian Wakem, Oct 21, 2007
    #2
    1. Advertising

  3. August Karlstrom

    Guest

    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.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
    , Oct 22, 2007
    #3
  4. 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
    Peter Makholm, Oct 22, 2007
    #4
  5. August Karlstrom

    Guest

    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:
    >
    , Oct 22, 2007
    #5
  6. August Karlstrom

    Guest

    On Oct 22, 12:34 pm, "" <>
    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
    , Oct 22, 2007
    #6
  7. 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.
    | | | |
    __/ | http://www.hjp.at/ | -- Sam in "Freefall"
    Peter J. Holzer, Oct 27, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rc
    Replies:
    3
    Views:
    1,860
    Chris Uppal
    Aug 16, 2004
  2. Miguel Manso
    Replies:
    1
    Views:
    584
    Wallace Owen
    Jun 15, 2006
  3. Fredrik Lundh
    Replies:
    1
    Views:
    1,175
    George Sakkis
    Nov 6, 2005
  4. Replies:
    4
    Views:
    233
    Paul Lalli
    Aug 1, 2007
  5. jidanni

    Data::Dumper vs. UTF-8, as usual

    jidanni, Mar 21, 2011, in forum: Perl Misc
    Replies:
    5
    Views:
    272
    Ilya Zakharevich
    Mar 23, 2011
Loading...

Share This Page