How do I define new make tasks, using `Makefile.PL`?

202 Views Asked by At

I would like my generated Makefile to have these new tasks for linting:

perl:
    -for f in **/*.pl; do perl -MO=Lint -cw $$f 2>&1 | grep -v "syntax OK"; done
    -for f in **/*.pm; do perl -MO=Lint -cw $$f 2>&1 | grep -v "syntax OK"; done

perlcritic:
    -perlcritic . | grep -v "source OK"

lint: perl perlcritic

I tried writing a Makefile.PL, but when I run it, the resulting Makefile still lacks the lint task.

use ExtUtils::MakeMaker;

sub MY::lint {
  return <<'END';
lint:
    echo "Linting!!!!!!!!!!!1"

END
}

WriteMakefile;

I tried reading the CPAN docs, but like most docs, they give snippets without sufficient context, so I'm not even sure if I should declare the subs before or after WriteMakefile.

Also posted on Reddit.

2

There are 2 best solutions below

1
On

Thanks to briandfoy:

$ cat Makefile.PL 
#!/usr/bin/env perl

use strict;
use warnings;
use ExtUtils::MakeMaker;

WriteMakefile;

sub MY::postamble {
  return <<'END';
perlwarn:
    -find . -type f -name '*.pl' -exec perl -MO=Lint -cw {} 2>&1 \; | grep -v "syntax OK" | grep -v "Can't locate"
    -find . -type f -name '*.pm' -exec perl -MO=Lint -cw {} 2>&1 \; | grep -v "syntax OK" | grep -v "Can't locate"
    -find . -type f -name '*.t' -exec perl -MO=Lint -cw {} 2>&1 \; | grep -v "syntax OK" | grep -v "Can't locate"

perlcritic:
    -perlcritic . | grep -v "source OK"

lint: perlwarn perlcritic
END
}
0
On

A solution that works and is slightly easier to maintain is to put the make targets in a separate makefile, so you can benefit of your text editor capabilities and it is a bit easier to read:

# In Makefile.PL
use File::Slurp;

sub MY::postamble {
    my $targets = read_file('./script/additional.make');
    return $targets;
}

# In /script/additional.make
perl:
    for f in **/*.pl; do perl -MO=Lint -cw $$f 2>&1 | grep -v "syntax OK"; done
    for f in **/*.pm; do perl -MO=Lint -cw $$f 2>&1 | grep -v "syntax OK"; done

perlcritic:
    perlcritic . | grep -v "source OK"

lint: perl perlcritic

Note for later readers: I am using Module::Install and it was necessary to use :: as separators because it seems that Module::Install forbid to mix : and ::. It also disallow the use of -.