Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

check syntax of perl code and pod in emacs

by InfiniteLoop (Hermit)
on Oct 11, 2004 at 14:13 UTC ( [id://398159] : sourcecode . print w/replies, xml ) Need Help??
Category: Miscellaneous
Author/Contact Info

If you use emacs, this function will check the syntax of your perl code and also run podchecker against it and display the result in a new buffer. You can call this function via the after-save-hook, and have the syntax checker run automatically. The function checks for CPerl mode.

Updated the code and the 'perlsyn' folder is shown only when there is a syntax error.(thanks to water)

Updated code to do a perl -wc (thanks to htoug)

;;my after-save-hook to cperl
(defun myperl-check-syntax ()
  (if (not (equal mode-name "CPerl"))
    ;;get the full path of this file
    (setq source-full-path (buffer-file-name))
    ;;clear the buffer of earlier error
    (set-buffer (get-buffer-create "perlsyn"))
    ;;set the perl5lib
    (setenv "PERL5LIB" "/usr/lib/perl5")
    ;;execute perl -wc
    (call-process "perl" nil "perlsyn" nil "-wc" source-full-path)
    ;;execute podchecker
;;    (call-process "podchecker" nil "perlsyn" nil 
;;                                                source-full-path)
    ;; checks if perl returned a syntax OK string
    (if (not (equal (substring (buffer-string) -3 -1) "OK"))
    (switch-to-buffer "perlsyn")))

(add-hook 'after-save-hook 'myperl-check-syntax)
Replies are listed 'Best First'.
Re: check syntax of perl code and pod in emacs
by water (Deacon) on Oct 12, 2004 at 00:39 UTC
    Many thanks!

    I am not a lisp or emacs guru -- how might one change the code so that it jumped to the error check buffer only when an error was found, and didn't jump to that buffer if the checked code was clean?

Re: check syntax of perl code and pod in emacs
by stefan k (Curate) on Oct 12, 2004 at 09:48 UTC
    on my system I can M-x cperl-check-syntax which opens a compilation buffer (with broken font-lock) in which I can then jump to the found error(s) directly.

    Regards Stefan

    Regards... Stefan
    you begin bashing the string with a +42 regexp of confusion

      Sorry for all this lisp stuff, but when I
      M-x cperl-check-syntax
      I get
      Cannot open load file: mode-compile
      Again, apologies for being so emacs clueless.

      Also, why, Stefan, is the method suggested by this thread not preferred? If the code to be checked involved extensive external modules, wouldn't the 'brute force' syntax check of this method better catch and display any problems using those external dependencies?

      Thanks for the insight.


        to your first problem: mode-compile ships with XEmacs (which I use) but doesn't seem to be shipped with Gnu Emacs by default (at least not on my system and the (almost) latest download I have lying around. You can try which seems to be the official download site, but I can't reach it right now. Other than that there are old versions distributed in elisp-archives (search for mode-compile.el in your favorite search machine ;-) Or you may try the XEmacs version that may be on your system, already.

        Once you downloaded it, follow the install instructions in that file. You may skip the byte-compiling which only improves speed, but you have to put the file in one of the directories that the variable load-path holds. Expand you load-path as you like:

        (setq load-path (append (list "~/elisp") load-path))
        As to your second question: Although I appreciate, like and do it myself I'd almost always prefer a well-tested and hopefully maintained piece of code from someone else to one quick written hack by myself. What would you do when the other code doesn't do what you want? Well, you'd done it already: get back to the Monastery and post a question. Whereas one could hope that the emacs and xemacs newsgroups and/or the cperl-mode and mode-compile maintainer might do a better job. But this is just my way of handling things. If the code works for you, go ahead and use it.

        Ah, and just as a sidenote: you might try getting used to writing emacs-lisp-code. It's fun ;-)

        One last: it's stefan k, not Stefan (but I admit that my sig is confusing here)

        Regards... Stefan
        you begin bashing the string with a +42 regexp of confusion

Re: check syntax of perl code and pod in emacs (warnings)
by htoug (Deacon) on Oct 12, 2004 at 07:32 UTC
    You should replace the "-c" with "-wc" to ensure that warnings are enabled - just in case someone forgets use warnings; or uses an older perl.

    Janitored by Arunbear - retitled from 'Warnings...'

Re: check syntax of perl code and pod in emacs
by vjo (Novice) on Oct 20, 2004 at 21:23 UTC
    Let me (again) recommend flymake

    It perfoms "perl -cw" checking in the background when you are idle (not typing). Like a spell-checker it highlights your errors, so you can easily spot them (and are not forced to do explicit compiles).

    The myperl-check-syntax function could be improved by changing the last if-expression to:
      (when (not (equal (substring (buffer-string) -3 -1) "OK"))
            (switch-to-buffer "perlsyn")
            (goto-char (point-min))))

    This enables font-locking and the "hyperlink" feature of the error lines.