From 21babbceb1532793695ee903d7e5e4ec7c98ba39 Mon Sep 17 00:00:00 2001 From: Cesar Tessarin Date: Mon, 23 Oct 2017 07:16:56 -0200 Subject: [PATCH] Fix Perl 5 and 6 disambiguation bug (#3860) * Add test to demonstrate Perl syntax detection bug A Perl 5 .pm file containing the word `module` or `class`, even with an explicit `use 5.*` statement, is recognized as Perl 6 code. * Improve Perl 5 and Perl 6 disambiguation The heuristics for Perl 5 and 6 `.pm` files disambiguation was done searching for keywords which can appear in both languages (`class` and `module`) in addition to the `use` statement check. Due to Perl 6 being tested first, code containing those words would always be interpreted as Perl 6. Test order was thus reversed, testing for Perl 5 first. Since Perl 6 code would never contain a `use 5.*` statement, this does no harm to Perl 6 detection while fixing the problem to Perl 5. Fixes: #3637 --- lib/linguist/heuristics.rb | 6 +++--- test/fixtures/Perl/Module.pm | 8 ++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/Perl/Module.pm diff --git a/lib/linguist/heuristics.rb b/lib/linguist/heuristics.rb index f688312a..c9d771fd 100644 --- a/lib/linguist/heuristics.rb +++ b/lib/linguist/heuristics.rb @@ -358,10 +358,10 @@ module Linguist end disambiguate ".pm" do |data| - if /^\s*(?:use\s+v6\s*;|(?:\bmy\s+)?class|module)\b/.match(data) - Language["Perl 6"] - elsif /\buse\s+(?:strict\b|v?5\.)/.match(data) + if /\buse\s+(?:strict\b|v?5\.)/.match(data) Language["Perl"] + elsif /^\s*(?:use\s+v6\s*;|(?:\bmy\s+)?class|module)\b/.match(data) + Language["Perl 6"] elsif /^\s*\/\* XPM \*\//.match(data) Language["XPM"] end diff --git a/test/fixtures/Perl/Module.pm b/test/fixtures/Perl/Module.pm new file mode 100644 index 00000000..d930cc66 --- /dev/null +++ b/test/fixtures/Perl/Module.pm @@ -0,0 +1,8 @@ +use 5.006; +use strict; + +=head1 + +module + +=cut