English 中文(简体)
参考文献
原标题:not array reference with perl XML::Simple code
  • 时间:2011-10-05 03:03:47
  •  标签:
  • xml
  • perl

我先是测试<代码>XML:Simple ,其中有一些xml文档产出,并且已经注意到,如果我有超过(1)个档案途径,我的文字将发挥作用(2.xml)。

If I have only (1) file path (1.xml) then I will get this message: Not an ARRAY reference at ./tst-simple.pl line 28

我的问题是,为什么在(1)档案道路上赢得法律工作? 我正在使用Array=>1。 我如何处理这一问题?

<>strong>1.xml:

<?xml version="1.0" encoding="UTF-8"?>
<listing time="2011-10-04T02:33:44+0000" recursive="no" path="/storage/foobar/test/queues/20110731" exclude="" filter=".*" version="0.20.202.1.1101050227">
    <directory path="/storage/foobar/test/queues/20110731" modified="2011-10-04T02:32:11+0000" accesstime="1970-01-01T00:00:00+0000" permission="drwx------" owner="unix_act" group="foobar"/>
    <file path="/storage/foobar/test/queues/20110731/myfilename-00" modified="2011-10-03T04:47:46+0000" accesstime="2011-10-03T04:47:46+0000" size="123456789" app="3" blocksize="134217728" permission="-rw-------" owner="unix_act" group="foobar"/>
</listing>

<>strong>2.xml:

<?xml version="1.0" encoding="UTF-8"?>
<listing time="2011-10-04T02:33:44+0000" recursive="no" path="/storage/foobar/test/queues/20110731" exclude="" filter=".*" version="0.20.202.1.1101050227">
    <directory path="/storage/foobar/test/queues/20110731" modified="2011-10-04T02:32:11+0000" accesstime="1970-01-01T00:00:00+0000" permission="drwx------" owner="unix_act" group="foobar"/>
    <file path="/storage/foobar/test/queues/20110731/myfilename-00" modified="2011-10-03T04:47:46+0000" accesstime="2011-10-03T04:47:46+0000" size="123456789" app="3" blocksize="134217728" permission="-rw-------" owner="unix_act" group="foobar"/>
    <file path="/storage/foobar/test/queues/20110731/myfilename-01" modified="2011-10-03T04:47:46+0000" accesstime="2011-10-03T04:47:46+0000" size="123456789" app="3" blocksize="134217728" permission="-rw-------" owner="unix_act" group="foobar"/>
</listing>

<><>Code>:

use strict;
use warnings;
use XML::Simple;
use Data::Dumper;

my $xml = $ARGV [0]; 
my $data = XMLin($xml); 

for my $xs ($xml) {
    #my $data = XMLin($xs, ForceArray => 0);
    my $data = XMLin($xs, ForceArray => 1);
    #my $data = XMLin($xs, ForceArray => [ qw ( path ) ]);
    print Dumper ($data);
}

foreach my $file( @{ $data->{file} } )
{
    my( $dir, $fname );
    if( $file->{path} =~ /^(.*)/([^/]+)$/ )
    {
        $dir = $1;
        $fname = $2;
    }
    else 
    {
        $dir = "";
        $fname = $file->{path};
    }
    print "This is the DIRECTORY: $dir
"; 
    print "This is the FILE:      $fname
";
    print "This is the FILE SIZE: $file->{size}
";
}
最佳回答

你们有两个不同的变量,即<代码> 数据。 其中一个是使用<条码>强迫Array => 1创建的,但你使用在没有使用<条码”的情况下创建的代码。 ForceArray => 1。

替换

my $data = XMLin($xml); 

for my $xs ($xml) {
    #my $data = XMLin($xs, ForceArray => 0);
    my $data = XMLin($xs, ForceArray => 1);
    #my $data = XMLin($xs, ForceArray => [ qw ( path ) ]);
    print Dumper ($data);
}

iii

my $data = XMLin($xml, ForceArray => 1);
print(Dumper($data));
问题回答

暂无回答




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签