We have been using Perl::Critic here at work to enforce our code conventions. Recently we ran into issues with /tmp directory getting filled up due to the Temp::File::tempdir function. tempdir cleans up when the Perl process terminates, but since our entire backend is a Perl process, this only occurs when the server itself gets restarted (not very often). We want to encourage developers to use the newdir object method in the future, which cleans up after itself as soon as the object goes out of scope.
Basically, we’re trying to mark Temp::File::tempdir as a code convention violation, but I can’t seem to find any rule that would be similar on CPAN. I understand that this is hard to enforce in a dynamically-typed language without introducing false positives, but I would expect someone has ran into similar issue in the past with another deprecated function. We’re not expecting to catch all the tricky cases either, only the most obvious uses of Temp::File::tempdir. The idea is to discourage accidental use of tempdir when newdir could do the job, not to catch all attempts to fool the critic (developer could always just use ## no critic). It would probably be enough to complain when tempdir is used if use Temp::File is defined (preferably checking that nothing else redefines tempdir) and when Temp::File::tempdir is used.
Is there already something similar, or should I start from scratch? Thanks
There isn’t anything in
Perl::Criticat present to provide what you need, but it’s quite possible add a policy to do something like it. UnfortunatelyPPIisn’t comprehensive enought to identify properly what each token in the program is doing, so it takes more coding than it might.This program checks for a
use File::Tempstatement that tries to importtempdirusing any of(with any delimiter for the
q,qq, andqwforms). It also checks for aPPI::Token::Wordnode that looks like a function call and is equal toFile::Temp::tempdir.with this code
generates this output from
perlcritic -4 test.pl