Skip to content

Instantly share code, notes, and snippets.

@berekuk
Created December 2, 2010 19:18
Show Gist options
  • Save berekuk/725880 to your computer and use it in GitHub Desktop.
Save berekuk/725880 to your computer and use it in GitHub Desktop.
diff --git a/Changes b/Changes
index f25ec46..bc3ad3e 100644
--- a/Changes
+++ b/Changes
@@ -1,6 +1,7 @@
Revision history for Perl-PrereqScanner
{{$NEXT}}
+ - throw an exception if PPI fails to parse code
0.101891 2010-09-05 15:31:49 America/New_York
- add a core scanner for Test::More's done_testing
diff --git a/lib/Perl/PrereqScanner.pm b/lib/Perl/PrereqScanner.pm
index 1c1a81e..fb2255e 100644
--- a/lib/Perl/PrereqScanner.pm
+++ b/lib/Perl/PrereqScanner.pm
@@ -55,11 +55,15 @@ sub BUILD {
Given a string containing Perl source code, this method returns a
Version::Requirements object describing the modules it requires.
+This method will throw an exception if PPI fails to parse the code.
+
=cut
sub scan_string {
my ($self, $str) = @_;
my $ppi = PPI::Document->new( \$str );
+ confess "PPI parse failed" unless defined $ppi;
+
return $self->scan_ppi_document( $ppi );
}
@@ -71,11 +75,15 @@ sub scan_string {
Given a file path to a Perl document, this method returns a
Version::Requirements object describing the modules it requires.
+This method will throw an exception if PPI fails to parse the code.
+
=cut
sub scan_file {
my ($self, $path) = @_;
my $ppi = PPI::Document->new( $path );
+ confess "PPI failed to parse '$path'" unless defined $ppi;
+
return $self->scan_ppi_document( $ppi );
}
diff --git a/t/autoprereq.t b/t/autoprereq.t
index ba9556e..3a12ca7 100644
--- a/t/autoprereq.t
+++ b/t/autoprereq.t
@@ -213,4 +213,14 @@ prereq_is(
},
);
+{
+ my $scanner = Perl::PrereqScanner->new;
+ try {
+ $scanner->scan_string(\"\x0");
+ fail('scan succeeded');
+ } catch {
+ like($_, qr/PPI parse failed/);
+ };
+}
+
done_testing;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment