开发者

How can I save XML::Simple to Hashes? The XML is changed, so cannot base one attr to use

I have a dump from Data::Dumper when using XML::Simple like this:

$VAR1 = {
    'web' => {
        'cmd' => {'sw_package' => ['test_zipfs', 'test_ini']},
        'bsp' => {
            'dir'      => '.',
            'type'     => 'uc',
            'dir'      => 'soft/web/bsp',
            'test_ini' => 'lan'
        },
    },
    'world' => {
        'cmd' => {'undef' => 'undef'},
        'bsp' => {
            'dir'  => '.',
            'type' => 'hale',
            'dir'  => 'soft/hel/bp'
        },
    },
};

I want to copy {web} -> {cmd} to a hash and the same for bsp. I know can access the last stage of elements {web} -> {bsp} -> {dir}. But the XML is blind, meaning that I won't know exactly which elements it has. So I want only copy and save a开发者_如何转开发 hash.

I tried:

my $cmd = Dumper($data -> {web} -> {cmd});
my %cmd_hash = %$cmd;

But when use a loop through new hash %cmd_hash nothing happens, it doesn't print out anything.


If I'm understanding correctly, you want to make a deep copy of parts of the hash. There's lots of ways to do this; my preference is the Clone module.

use Clone qw(clone);
my $cmd_copy = clone($data->{web}->{cmd});
my $bsp_copy = clone($data->{web}->{bsp});

What you're doing doesn't work because Dumper produces a string, which you're then trying to use as a hash reference. If you have use strict 'refs' enabled then you'd get a warning like Can't use string ("cmd") as a HASH ref when you tried to dereference it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜