Flymake in Emacs Perl mode

ChrisDolan on 2006-11-07T19:24:09

Today while reading LWN, I learned about the Flymake minor mode for Emacs. This software does on-the-fly compile checking, kind of like the on-the-fly spell checking that has become popular in many editors.

Whenever Emacs becomes idle, flymake shells out and runs "perl -c" on your code. It parses the output for errors. Back in Emacs, flymake highlights the offending code. Take a look at a screenshot I created, showing flymake responding to a missing semicolon.

Flymake appears to be pre-installed under Emacs 22 ... well, at least it is pre-installed under the Emacs that I get via Fink on my Mac. I invoked it via "M-x flymake-mode"

Has anyone else tried this?

It seems nice on trivial code I've tested, but it seems to disable itself a little too readily. It bases its decisions purely on the filename extension implied by the buffer name, instead of checking in with the major mode. It has trouble guessing @INC paths too, as can be expected. For example, I tried it on some Perl::Critic code, but because 'lib' wasn't in @INC, flymake turned itself off after one compile failure.


perlcritic-fly.el????

jjore on 2006-11-07T19:58:17

I want this. I *so* want this. The thing that bothers me is that I'd like to do some sane defaults for @INC because it becomes very difficult to validate modules or things using modules that aren't installed by the ambient or shebang perl.

I'm thinking that just autoadding lib/, t/, uh... t/lib would be spiffy. Perhaps with some dwimmery to notice if those directories can be found by navigating up the directory tree:

    ~jjore/src/Foo-Bar/t$ ... # automatically find ../lib

Maybe there oughta be some data in a ~/.whatever file to allow a person to specify what happens when under certain paths.

Re:perlcritic-fly.el????

Abigail on 2006-11-08T09:58:30

I'm thinking that just autoadding lib/, t/, uh... t/lib would be spiffy.

That's a functionality that has been present in Perl for ages. It's called PERL5LIB.

Re:perlcritic-fly.el????

jjore on 2006-11-08T18:12:46

Oh, well duh. *slaps head*. I was going to look first and then populate PERL5LIB but I don't have to look first. Perl will do that for me.

Re:perlcritic-fly.el????

thaljef on 2006-11-17T03:45:41

I downloaded flymake.el from their repository, but I wasn't able to make it work with my Emacs 21.2.1. I don't have the lisp-fu to figure it out, so I'll probably just wait until Emacs 22 is released.

Putting aside the argument about compiling untrusted code, you should be able to get the effect you want by putting "use criticism;" at the top of your script. In the latest release of criticism.pm, we changed the default output format to look exactly like the perl's native compiler warnings. So when you run "perl -c SomeFile.pm" the Perl::Critic warnings blend right into the compiler warnings. For a while, this was how the EPIC guys were integrating with Perl::Critic.

If you're able to get flymake-mode working, please let me know if the "use criticism;" trick actually works.

-Jeff

Re:perlcritic-fly.el????

jjore on 2006-11-17T18:26:35

Ok... but `use criticism' only works when you run the code.

Re:perlcritic-fly.el????

thaljef on 2006-11-17T22:55:23

Not so. It is pragma-like, so it executes when you compile the code. Observe what happens when you say "perl -c" on a file with "use criticism;"

-Jeff

Re:perlcritic-fly.el????

jjore on 2006-11-17T23:06:30

Well yeah, that's what I said. -c is a form of running the code.

Re:perlcritic-fly.el????

thaljef on 2006-11-17T23:42:47

I thought that's what you were shooting for -- running perlcritic at compile time. But if not, then I understand. My apoligies.

Perhaps we could repurpose the flymake code and just substitute "perlcritic" where it says "perl -c". But your lisp skills are far better than mine.

-Jeff

Re:perlcritic-fly.el????

jjore on 2006-11-17T23:57:59

I thought you were proposing running perlcritic instead of perl -c + the criticism pragma. So it appears I was already at the endgame.

beware perl -c on unknown code

merlyn on 2006-11-08T02:43:50

All I have to do now is send you a perl script to "look at" that contains:
BEGIN { system 'rm -rf $HOME' }
and hope that you look at it in your editor. That flymake code will nicely execute that system operation. Oops!

Re:beware perl -c on unknown code

ChrisDolan on 2006-11-08T03:53:43

Yeah, I had thought of something like that. Yikes! :-)

On my mental todo list is to check if flymake does a check right after load, or only after a first edit.

Re:beware perl -c on unknown code

jplindstrom on 2006-11-10T16:43:57

That's customizable: "Flymake Start Syntax Check On Find File".

Re:beware perl -c on unknown code

jrockway on 2010-01-26T23:40:09

And then I just "git clone" my homedir again, and you go to prison again.

Re:beware perl -c on unknown code

ChrisDolan on 2010-01-27T02:38:17

I continue to be amazed at how friendly you are in person.

Re:beware perl -c on unknown code

educated_foo on 2010-01-27T03:25:04

I'd be amazed and pleasantly surprised by his not spitting on people in person. Who digs up a 3-year-old journal entry to take a cheap shot at someone related to something they did 15 years ago? I'm not sure that even greater internet fuckward theory can explain it.

Tried it

jplindstrom on 2006-11-10T16:29:54

I tried it, but out of the box it only recognized .pl files (can be customized to recognize .t and .pm of course) and -- more annoyingly -- whenever it found an error, it moved the point from wherever I was writing bad code to the first syntax error it could fine.

This invariably led to me typing something like:

my $my_long_vari

then pausing to think, and it would just pop me over to the closing ; somewhere completely uninteresting to me.

Way too intrusive in other words.

It seems that kind of thing should be customizable though.

Re:Tried it

jplindstrom on 2006-11-10T17:06:34

Ah, I accidently got an older version. Please disregard.