Hi everyone.
First of all, I am still a Perl newbie and I'm trying to figure out the wonders of this language.
Now that the pleasantries are over, let me submit to you my question : I am not able to catch errors thrown by the YAML module (YAML_PARSE_ERR_INCONSISTENT_INDENTATION, YAML_PARSE_ERR_NO_SEPARATOR...).
I have tried the classic :
my $var = LoadFile($fh) or warn/die "MESSAGE";
but I still cannot catch anything :
YAML Error: Inconsistent indentation level
Code: YAML_PARSE_ERR_INCONSISTENT_INDENTATION
Line: 21
Document: 1
at /usr/share/perl5/YAML/Loader.pm line 719
Here is my code :
#!/usr/bin/env perl
use strict;
use Getopt::Long;
use YAML qw(LoadFile);
use Data::Dumper;
my $file;
GetOptions (
"file=s" => \$file);
if ( $file ) {
$SIG{__DIE__} = sub { die("My error: ", @_); };
$SIG{__WARN__} = sub { warn("My warning: ", @_); };
open my $fh, '<', $file
or die "Cannot open target file\n"; #THIS ERROR IS CATCHABLE
my $config = LoadFile($fh)
or warn("YAML parsing error\n"); #CANNOT CATCH ERROR
#or warn "YAML parsing error\n"; #CANNOT CATCH ERROR
print Dumper(\$config), "\n";
} else {
die "Syntax: $0 [-f,--file] [YAML_FILE]";
}
Can you please provide with any help ?
Thank you in advance.
Regards.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.