in reply to "localise" package's our variables? They affect other packages (e.g. File::Temp::KEEP_ALL)

use File::Path qw( remove_tree ); use File::Temp qw( tempdir ); my $tmpdir = tempdir( CLEANUP => 0 ); ... remove_tree( $tmpdir ) if $success;

Test:

$ tt() { perl -e' use v5.14; use File::Path qw( remove_tree ); use File::Temp qw( tempdir ); my $tmpdir = tempdir( CLEANUP => 0 ); say $tmpdir; my $success = $ARGV[0]; remove_tree( $tmpdir ) if $success; ' -- "$@" } $ t() { local tmpdir=$( tt "$@" ) if [ -e "$tmpdir" ]; then echo Preserved rm -r -- "$tmpdir" else echo Cleaned fi } $ t 0 Preserved $ t 1 Cleaned
  • Comment on Re: "localise" package's our variables? They affect other packages (e.g. File::Temp::KEEP_ALL)
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: "localise" package's our variables? They affect other packages (e.g. File::Temp::KEEP_ALL)
by bliako (Abbot) on Oct 11, 2024 at 11:24 UTC

    Thank you. It is far from elegant (not your fault:)) but perhaps my test files will be templated on the below from now on. The question still remains though re: global variables in packages ...

    ################################################################### #### NOTE env-var TEMP_DIRS_KEEP=1 will stop erasing tmp files ################################################################### use strict; use warnings; our $VERSION = '1.01'; use Test::More; use Test::More::UTF8; use FindBin; use File::Temp 'tempdir'; use File::Path qw( remove_tree ); my $curdir = $FindBin::Bin; my $tmpdir = File::Temp::tempdir( CLEANUP=>0, TEMPLATE => 'ooooooooXXXX' ); ok(-d $tmpdir, "tmpdir exists $tmpdir") or BAIL_OUT; #die 123; # this should keep the tmpdir intact for inspection #BAIL_OUT('bailing out'); # this should keep the tmpdir intact for ins +pection # if you set env var TEMP_DIRS_KEEP=1 when running # the temp files WILL NOT BE DELETED otherwise # they are deleted automatically diag "temp dir: $tmpdir ..."; do { remove_tree( $tmpdir ); diag "temp files cleaned!"; } unless exists($ENV{'TEMP_DIRS_KEEP'}) && $ENV{'TEMP_DIRS_KEEP'}>0; # END done_testing;
      This shows nicely why I hate state variables in named subroutines. You can't call the subroutine from anywhere else without touching the same value.

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      It is far from elegant

      huh? It's the very definition of elegance. It's trivially simple, and very clearly says what it does.

        I meant adding one more module and then create the tempdir with one module and delete it with another. Hey!

Re^2: "localise" package's our variables? They affect other packages (e.g. File::Temp::KEEP_ALL)
by ikegami (Patriarch) on Oct 11, 2024 at 01:07 UTC

    The parent is NOT a dupe. It is a different solution than the other similar post.

      sorry; the posts looked rather similar to me, and I thought the code differences could be explained by the "update: needed to switch..." addenda to the first post -- I thought two originally got posted, then you edited the first; I was apparently wrong. I'll be more careful in DUP-consideration in the future.