我先是测试<代码>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}
";
}