Subject: | Inappropriate croak on syntax errors in parse(*OPENFILEHANDLE) [PATCH] |
Date: | Wed, 14 Jan 2015 15:32:13 -0800 |
To: | bug-XML-Twig [...] rt.cpan.org |
From: | Jim Avera <jim.avera [...] gmail.com> |
Hi,
The parse() method can be called with either a literal text string or an
open file handle.
If called with an open fh then syntax errors cause an inappropriate
croak with a message saying the wrong method was called (and the syntax
error message is not shown).
The following code is the culprit:
if( !$t
&& $@=~m{(syntax error at line 1, column 0, byte 0|not
well-formed \(invalid token\) at line 1, column 1, byte 1)}
&& -f $_[0]
)
{ croak "you seem to have..." }
I think the intention is to detect when the user passed a pathname to
parse(), in which case parsefile() should have been called. However
Perl's -f operator works on pathnames OR open file handles, so the test
succeeds when a file handled is passed.
This can be fixed by concatenating "" to the argument to -f, so that an
open file handle will become a plain string like "*main::FILE" and the
test will fail unless there's a strange file named like that.
Here's a patch for Twig.pm version 2.23.
Thanks for the great software!
-Jim Avera
--- Twig.pm.ORIG 2015-01-14 14:57:18.703778966 -0800
+++ Twig.pm 2015-01-14 15:24:44.202484250 -0800
@@ -762,7 +762,7 @@
if( !$t
&& $@=~m{(syntax error at line 1, column 0, byte 0|not
well-formed \(invalid token\) at line 1, column 1, byte 1)}
- && -f $_[0]
+ && -f $_[0].""
)
{ croak "you seem to have used the parse method on a filename
($_[0]), you probably want parsefile instead"; }
return _checked_parse_result( $t, $@);