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

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;
  • Comment on Re^2: "localise" package's our variables? They affect other packages (e.g. File::Temp::KEEP_ALL)
  • Download Code

Replies are listed 'Best First'.
Re^3: "localise" package's our variables? They affect other packages (e.g. File::Temp::KEEP_ALL)
by choroba (Cardinal) on Oct 11, 2024 at 11:28 UTC
    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]
Re^3: "localise" package's our variables? They affect other packages (e.g. File::Temp::KEEP_ALL)
by ikegami (Patriarch) on Oct 11, 2024 at 11:33 UTC

    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!

        The module in question is a core module, and it's already being loaded by File::Temp.