richterger / Perl-LanguageServer

Language Server for Perl
Other
224 stars 53 forks source link

Fix FindBin and $0 in code #16

Closed bmartin closed 2 years ago

bmartin commented 4 years ago

I was issue #2 able to fix this in my copy by adding:

--- a/Perl/LanguageServer/SyntaxChecker.pm
+++ b/Perl/LanguageServer/SyntaxChecker.pm
@@ -140,6 +140,8 @@ sub background_checker
         $text = eval { Encode::encode ('utf-8', $text) ; } ;
         $self -> logger ($@) if ($@) ;    

+        my ($file) = ($uri =~ m{^file://(.*)});
+        $text = "local \$0; BEGIN { \$0 = '$file'; if (\$INC{'FindBin.pm'}) { FindBin->again(); } }\n# line 1 \"$file\"\n" . $text;
         my $ret ;
         my $errout ;
         my $out ;

This fixes $0, FindBin:*, and __FILE__. See perlsyn Plain-old-comment (not!) for how it fixes __FILE__ and __LINE__.

It feels like this is probably function is probably the wrong place to put this fix.

Originally posted by @bmartin in https://github.com/richterger/Perl-LanguageServer/issues/2#issuecomment-602275026

bmartin commented 2 years ago

Thank you!