• このエントリーをはてなブックマークに追加

メモ

Data::Dump::dump() 後の文字列を eval して得られるものが想定してたものと違っていることを知らなくてハマったけど,ちょっとした修正で解決できたのでそのあたりのメモ.

確認した環境は次のとおりです:

  • perl-5.12.4
  • Data-Dump-1.22
  • ソースコード読んでません><
use 5.12.0;
use strict;
use warnings;
use Data::Dumper;
use Data::Dump qw/dump/;
 
$Data::Dumper::Terse = 1;
 
my %data = (
    'foo' => [ 'bar', 'baz' ],
    '-hoge' => { 'fuga' => 'piyo' },
    -abc => { -x => 0, -y => 10 },
);
 
 
my $d_Dump   = dump \%data;
my $d_Dumper = Dumper \%data;
 
say << "...";
### HashRef -> Data::Dump::dump()
$d_Dump
 
### HashRef -> Data::Dumper::Dumper()
$d_Dumper
 
### eval Data::Dump::dump()-ed (with Data::Dump::dump())
@{[ dump eval $d_Dump ]}
 
### eval Data::Dump::dump()-ed (with Data::Dumper::Dumper())
@{[ Dumper eval $d_Dump ]}
 
### eval Data::Dump::Dumper()-ed (with Data::Dumper::Dumper())
@{[ Dumper eval $d_Dumper ]}
 
### eval Data::Dump::Dumper()-ed (with Data::Dump::dump())
@{[ dump eval $d_Dumper ]}
...
### HashRef -> Data::Dump::dump()
{
  -abc  => { -x => 0, -y => 10 },
  -hoge => { fuga => "piyo" },
  foo   => ["bar", "baz"],
}
 
### HashRef -> Data::Dumper::Dumper()
{
  '-abc' => {
              '-y' => 10,
              '-x' => 0
            },
  'foo' => [
             'bar',
             'baz'
           ],
  '-hoge' => {
               'fuga' => 'piyo'
             }
}
 
 
### eval Data::Dump::dump()-ed (with Data::Dump::dump())
(
  "-abc",
  { -x => 0, -y => 10 },
  "-hoge",
  { fuga => "piyo" },
  "foo",
  ["bar", "baz"],
)
 
### eval Data::Dump::dump()-ed (with Data::Dumper::Dumper())
'-abc'
 {
  '-y' => 10,
  '-x' => 0
}
 '-hoge'
 {
  'fuga' => 'piyo'
}
 'foo'
 [
  'bar',
  'baz'
]
 
 
### eval Data::Dump::Dumper()-ed (with Data::Dumper::Dumper())
{
  '-abc' => {
              '-y' => 10,
              '-x' => 0
            },
  'foo' => [
             'bar',
             'baz'
           ],
  '-hoge' => {
               'fuga' => 'piyo'
             }
}
 
 
### eval Data::Dump::Dumper()-ed (with Data::Dump::dump())
{
  -abc  => { -x => 0, -y => 10 },
  -hoge => { fuga => "piyo" },
  foo   => ["bar", "baz"],
}

ちなみに,本来の挙動はこんな感じです:

my %data = (
    'foo' => [ 'bar', 'baz' ],
    'hoge' => { 'fuga' => 'piyo' },
    abc => { -x => 0, -y => 0 },
);
 
say << "...";
### dumped
@{[ dump \%data ]}
 
### eval dumped
@{[ Dumper eval dump \%data ]}
...
### dumped
{
  abc  => { -x => 0, -y => 0 },
  foo  => ["bar", "baz"],
  hoge => { fuga => "piyo" },
}
 
### eval dumped
{
  'abc' => {
             '-y' => 0,
             '-x' => 0
           },
  'foo' => [
             'bar',
             'baz'
           ],
  'hoge' => {
              'fuga' => 'piyo'
            }
}

- で始まるキーを含む HashRef で起こるようです:

とりあえず想定どおりになるようにするには, "+{dumped な文字列}" を eval すればよいっぽい:

$data = eval( "+$dumped" );
say << "...";
### eval Data::Dump::dump()-ed (expected)
@{[ Dumper eval "+$d_Dump" ]}
...
### eval Data::Dump::dump()-ed (expected)
{
  '-abc' => {
              '-y' => 10,
              '-x' => 0
            },
  'foo' => [
             'bar',
             'baz'
           ],
  '-hoge' => {
               'fuga' => 'piyo'
             }
}

これで大丈夫かな.

...4年くらい前に書いたアプリケーションが,これが原因で最近になって不具合起こしたので,アドホックな修正で対処.ほぼ 1日かかってしまいました ><