2

I would like to have a quick way to check the syntax of the current Perl file. Suppose we have following incorrect file:

#!/usr/bin/perl
sub aaa {
    dfgdf dfgdfg 
    if ) {}
       
sub bbb { }

When we run perl -c ./test.pl from terminal we get:

syntax error at ./test.pl line 5, near "if ) "
Missing right curly or square bracket at ./test.pl line 7, at end of line
./test.pl had compilation errors.

Ideally, I would like to have quickfix window to be shown if errors are present:

syntax error near "if ) "                            <- points to ./test.pl line 5
Missing right curly or square bracket at end of line <- points to ./test.pl line 7

I created the following function:

function! CheckPerlSyntax()
    let mp = &makeprg
    let ef = &errorformat
    let exeFile = expand("%:t")
    setlocal makeprg=perl\ -c\ %
    set efm=%m\ at\%f\ line\ %l
    make
    " copen
    let &makeprg     = mp
    let &errorformat = ef
endfunc

The algorithm was:

  1. Change make command to run perl -c current-file
  2. Parse the error messages
  3. Populate quickfix list from the
  4. Show the quickfix window

What I got so far:

  1. "perl -c" really runs, but I see the terminal output, is it possible to run it silently? I tried silent
  2. The parsing is incorrect, I can't jump to the line from the list.
  3. Will it be wise to set make program and errorformat for all the perl files as autocommand and not set and reset it in this function?
  4. Is it possible to populate the local list and not the global quickfix list?
8
  • BTW, there are plugins that do this and do it well. Personally, I use vim-ale but there are others. Linting using perl -c should work out of the box and you can configure whether to use quickfix or loclist.
    – Friedrich
    Commented May 1 at 12:23
  • And there's a built-in perl compiler plugin which seems to do everything except running silently. See :help compiler-perl.
    – Friedrich
    Commented May 1 at 18:21
  • Do you still have something open in your question? How can we help you further? Otherwise maybe could you accept one of the answers using the v button next to the arrow voting buttons. It allow the question to rest :-) Commented May 3 at 8:24
  • 1
    @VivianDeSmedt Oh, dear. I tried, but didn't fully finish the syntax checking process. I'll accept the answer, so it won't distract you.
    – user4035
    Commented May 14 at 10:04
  • 1
    Thanks for the feedback :-) Feel free to comment my answer if you experience problem or to open another question. We will do our best to help. Commented May 14 at 10:51

1 Answer 1

1

To have your solution automatically for perl files you could have a ~/.vim/after/ftplugin/perl.vim file with the following content:

setlocal makeprg=perl\ -c
setlocal errorformat=%m\ at\ %f\ line\ %l%.%#

The command to run to check the syntax of the current file is:

:silent make %

Remark: the errorformat must match the entire line (that is why it ends with %.%# which is internally translated to the .* pattern)

Remark:

  • If you run Neovim the file is probably ~/.config/nvim/after/ftplugin/perl.vim
  • If you run Vin on Windows the file is probably ~/vimfiles/after/ftplugin/perl.vim
11
  • unfortunately, my efm is incorrect. Can you try on your machine, so it could parse the error lines?
    – user4035
    Commented May 1 at 11:17
  • 2
    I tried :silent make before writing the question: for me it makes all the foreground color of fonts black, making the buffer with source code unreadable. Also it creates a new tab.
    – user4035
    Commented May 1 at 12:36
  • 2
    I tried :silent make %, yes. OS: Linux Slackware 14.2, vim version: VIM - Vi IMproved 9.1 (2024 Jan 02, compiled Mar 8 2024 09:29:09)
    – user4035
    Commented May 1 at 12:40
  • 1
    I tried also in pure terminal. The text is does not become black there, but the output from perl -c bacome shown at the bottom: "./test.pl\nsyntax error at ./test.pl..."
    – user4035
    Commented May 1 at 12:46
  • 2
    I'd much rather put the makeprg/errorformat definitions in a compiler plugin
    – D. Ben Knoble
    Commented May 1 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.