How to stop perl from exiting
Here's how to stop perl from exiting: use eval (a subset of the rule RTFM)! Here's some test code:
And finally here's the output:
This is bad.xml:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use XML::Simple;
my $loc = "/tmp/bad.xml";
my $xmldata = eval { XMLin($loc) };
if ($@) {
print "problem with xmldata in $loc, skip it\n";
} else {
print Dumper($xmldata);
}
$loc = "/tmp/good.xml";
$xmldata = eval { XMLin($loc) };
if ($@) {
print "problem with xmldata in $loc, skip it\n";
} else {
print Dumper($xmldata);
}
<metadata>And this is good.xml:
<file name="hello.file">
Simon & Garfunkel
</file>
</metadata>
<metadata>
<file name="hello.file">
Simon & Garfunkel
</file>
</metadata>
And finally here's the output:
wereldmuis@hostname:~$ perl testing.pl
problem with xmldata in /tmp/bad.xml, skip it
$VAR1 = {
'file' => {
'content' => '
Simon & Garfunkel
',
'name' => 'hello.file'
}
};
0 Comments:
Post a Comment
<< Home