Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8927321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:12:03+00:00 2026-06-15T08:12:03+00:00

We have been using Perl::Critic here at work to enforce our code conventions. Recently

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:12:04+00:00Added an answer on June 15, 2026 at 8:12 am

    There isn’t anything in Perl::Critic at present to provide what you need, but it’s quite possible add a policy to do something like it. Unfortunately PPI isn’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::Temp statement that tries to import tempdir using any of

    use File::Temp 'tempdir';
    use File::Temp q(tempdir);
    use File::Temp "tempdir";
    use File::Temp qq(tempdir);
    use File::Temp qw/ tempdir /;
    

    (with any delimiter for the q, qq, and qw forms). It also checks for a PPI::Token::Word node that looks like a function call and is equal to File::Temp::tempdir.

    package Perl::Critic::Policy::Prohibit_tempdir;
    
    use strict;
    use warnings;
    
    use Perl::Critic::Utils qw{ is_function_call :severities };
    use Scalar::Util 'blessed';
    
    use base 'Perl::Critic::Policy';
    
    my $DESC = 'Temp::File::tempdir function';
    my $EXPL = 'The tempdir function from Temp::File is deprecated. Use newdir method instead';
    
    sub default_severity { $SEVERITY_HIGH };
    
    sub applies_to{ qw/ PPI::Statement::Include PPI::Token::Word / }
    
    sub violates {
    
      my ($self, $elem) = @_;
    
      if ($elem->isa('PPI::Statement::Include')) {
    
        return unless $elem->type eq 'use';
    
        my $module = $elem->module;
        return unless $module and $module eq 'File::Temp';
    
        for my $kid ($elem->children) {
    
          next unless blessed($kid) =~ /^PPI::Token::Quote/;
    
          if ($kid->can('string') and $kid->string eq 'tempdir'
              or $kid->can('literal') and grep $_ eq 'tempdir', $kid->literal) {
            return $self->violation($DESC, $EXPL, $elem);
          }
        }
      }
      else {
        if (is_function_call($elem) and $elem eq 'File::Temp::tempdir') {
          return $self->violation($DESC, $EXPL, $elem);
        }
      }
    
      return;
    }
    
    1;
    

    with this code

    use strict;
    use warnings;
    
    use File::Temp 'tempdir';
    use File::Temp "tempdir";
    use File::Temp qw/ tempdir /;
    
    my $dir = tempdir();
    $dir = tempdir;
    $dir = File::Temp::tempdir;
    
    my $ft = File::Temp->new;
    $dir = $ft->newdir;
    

    generates this output from perlcritic -4 test.pl

    Code not contained in explicit package at line 1, column 1.  Violates encapsulation.  (Severity: 4)
    Temp::File::tempdir function at line 4, column 1.  The tempdir function from Temp::File is deprecated. Use newdir method instead.  (Severity: 4)
    Temp::File::tempdir function at line 5, column 1.  The tempdir function from Temp::File is deprecated. Use newdir method instead.  (Severity: 4)
    Temp::File::tempdir function at line 6, column 1.  The tempdir function from Temp::File is deprecated. Use newdir method instead.  (Severity: 4)
    Temp::File::tempdir function at line 10, column 8.  The tempdir function from Temp::File is deprecated. Use newdir method instead.  (Severity: 4)
    Module does not end with "1;" at line 13, column 1.  Must end with a recognizable true value.  (Severity: 4)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been using Perl for some time, but today I came across this
I have been using grep with perl extension for multiline match .However it turns
I have been trying to write a bare-bones ping scanner using Perl for internal
I have been using an API to do some work. This is how I
We have been using Perl's Net:Twitter CPAN module (version 3.12) and basic authentication (not
I've been using Perl for some time and have gotten used to the syntax:
I have been using Andrew Whitaker's code at jsfiddle.net/5xbhY. I was hoping someone could
We have a relatively standard SVN server in our office, and I've been using
I have been using PerlXS to write a perl wrapper around a C++ Object.
I have been trying to create a program using perl www::Mechanize that will follow

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.