From 3d3fc8282be218429403d7a4132c08628b4a42bd Mon Sep 17 00:00:00 2001 From: Yves Orton Date: Mon, 13 Mar 2023 12:30:38 +0100 Subject: [PATCH] Test2::Util - add _env_get() to wrap %ENV access. A test might lock %ENV, so we should always check for key existence before reading a key, or the lookup might trigger an exception. This function wraps the check up and provides a default facility to make logic that needs to consult %ENV more robust. --- lib/Test2/Util.pm | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/Test2/Util.pm b/lib/Test2/Util.pm index 9ce991ccf..a19e5a219 100644 --- a/lib/Test2/Util.pm +++ b/lib/Test2/Util.pm @@ -36,6 +36,8 @@ our @EXPORT_OK = qw{ try_sig_mask clone_io + + _env_get }; BEGIN { require Exporter; our @ISA = qw(Exporter) } @@ -43,6 +45,17 @@ BEGIN { *IS_WIN32 = ($^O eq 'MSWin32') ? sub() { 1 } : sub() { 0 }; } +# check for key existence before fetching from %ENV to avoid +# locked hash issues. If it does not exist, or it does and the +# value is undefined return $_[1] instead, thus allowing +# a default value to be supplied if required. +sub _env_get { + my ($key,$default) = @_; + my $got = exists($ENV{$key}) ? $ENV{$key} : undef; + $got = $default unless defined $got; + return $got; +} + sub _can_thread { return 0 unless $] >= 5.008001; return 0 unless $Config{'useithreads'};