<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">I think you have the condition backwards?</div><div>my $test_mode;</div><div>BEGIN { $test_mode=1 }</div><div>no if $test_mode, qw[warnings threads];</div><div><br></div><div dir="ltr"><br></div><div dir="ltr"><br><div><p style="margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">perldoc if</span></p></div><div><p style="margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures"><b>NAME</b></span></p>
<p style="margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">    if - "use" a Perl module if a condition holds</span></p>
<p style="margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0);min-height:14px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></p>
<p style="margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures"><b>SYNOPSIS</b></span></p>
<p style="margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">        use if CONDITION, "MODULE", ARGUMENTS;</span></p>
<p style="margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">        no  if CONDITION, "MODULE", ARGUMENTS;</span></p></div><div><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div><div><div dir="ltr" class="gmail_signature">-y<br></div></div><br></div></div></div></div></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Aug 1, 2020 at 3:10 AM David Christensen <<a href="mailto:dpchrist@holgerdanske.com">dpchrist@holgerdanske.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">sanfrancisco-pm:<br>
<br>
I have a Linux computer:<br>
<br>
2020-07-31 23:24:49 dpchrist@tinkywinky ~/sandbox/perl<br>
$ cat /etc/debian_version ; uname -a ; perl -v | head -n 3<br>
9.13<br>
Linux tinkywinky 4.9.0-12-amd64 #1 SMP Debian 4.9.210-1+deb9u1 <br>
(2020-06-07) x86_64 GNU/Linux<br>
<br>
This is perl 5, version 24, subversion 1 (v5.24.1) built for <br>
x86_64-linux-gnu-thread-multi<br>
(with 90 registered patches, see perl -V for more detail)<br>
<br>
<br>
I am working on a Perl 5 library that uses threads.  If code in a child <br>
thread throws an exception, I want the exception message to be printed; <br>
except during testing.<br>
<br>
<br>
I have found that:<br>
<br>
     no warnings 'threads'<br>
<br>
when placed before, and in the same block as, threads->create() will <br>
suppress printing of the exception message:<br>
<br>
2020-07-31 23:43:08 dpchrist@tinkywinky ~/sandbox/perl<br>
$ cat <a href="http://thread-exception.pl" rel="noreferrer" target="_blank">thread-exception.pl</a><br>
#!perl<br>
<br>
use strict;<br>
use warnings;<br>
use threads;<br>
<br>
use Test::More;<br>
use Test::Exception;<br>
<br>
our $thr;<br>
our $test_mode;<br>
<br>
sub foo<br>
{<br>
     if ($test_mode) {<br>
        no warnings 'threads';<br>
<br>
        $thr = threads->create(<br>
            sub { die "hello, world!" }<br>
        );<br>
     } else {<br>
        $thr = threads->create(<br>
            sub { die "hello, world!" }<br>
        );<br>
     }<br>
}<br>
<br>
ok foo, __FILE__ . __LINE__;                            # 1<br>
<br>
is $thr->join, undef, __FILE__ . __LINE__;              # 2<br>
<br>
like $thr->error, qr/hello/, __FILE__ . __LINE__;       # 3<br>
<br>
$test_mode = 1;<br>
<br>
ok foo, __FILE__ . __LINE__;                            # 4<br>
<br>
is $thr->join, undef, __FILE__ . __LINE__;              # 5<br>
<br>
like $thr->error, qr/hello/, __FILE__ . __LINE__;       # 6<br>
<br>
done_testing;<br>
<br>
2020-07-31 23:43:11 dpchrist@tinkywinky ~/sandbox/perl<br>
$ perl <a href="http://thread-exception.pl" rel="noreferrer" target="_blank">thread-exception.pl</a><br>
Thread 1 terminated abnormally: hello, world! at <a href="http://thread-exception.pl" rel="noreferrer" target="_blank">thread-exception.pl</a> <br>
line 23.<br>
ok 1 - thread-exception.pl28<br>
ok 2 - thread-exception.pl30<br>
ok 3 - thread-exception.pl32<br>
ok 4 - thread-exception.pl36<br>
ok 5 - thread-exception.pl38<br>
ok 6 - thread-exception.pl40<br>
1..6<br>
<br>
<br>
I was unable to figure out how to conditionally perform "no warnings <br>
'threads'" in the body of foo(), so I had to copy-and-paste the body <br>
into both halves of an if-else conditional and disable warnings in one <br>
block.  This is an ugly work-around.<br>
<br>
<br>
I was hoping for:<br>
<br>
     no warnings 'threads' if $test_mode;       # BAD CODE<br>
<br>
<br>
But, that code does not work;<br>
<br>
2020-07-31 23:56:50 dpchrist@tinkywinky ~/sandbox/perl<br>
$ cat <a href="http://no-warnings-threads-if-1.pl" rel="noreferrer" target="_blank">no-warnings-threads-if-1.pl</a><br>
#!perl<br>
<br>
use strict;<br>
use warnings;<br>
<br>
no warnings 'threads' if 1;<br>
<br>
2020-07-31 23:56:53 dpchrist@tinkywinky ~/sandbox/perl<br>
$ perl <a href="http://no-warnings-threads-if-1.pl" rel="noreferrer" target="_blank">no-warnings-threads-if-1.pl</a><br>
syntax error at <a href="http://no-warnings-threads-if-1.pl" rel="noreferrer" target="_blank">no-warnings-threads-if-1.pl</a> line 6, near "'threads' if"<br>
Execution of <a href="http://no-warnings-threads-if-1.pl" rel="noreferrer" target="_blank">no-warnings-threads-if-1.pl</a> aborted due to compilation errors.<br>
<br>
<br>
Any comments or suggestions?<br>
<br>
<br>
David<br>
_______________________________________________<br>
SanFrancisco-pm mailing list<br>
<a href="mailto:SanFrancisco-pm@pm.org" target="_blank">SanFrancisco-pm@pm.org</a><br>
<a href="https://mail.pm.org/mailman/listinfo/sanfrancisco-pm" rel="noreferrer" target="_blank">https://mail.pm.org/mailman/listinfo/sanfrancisco-pm</a><br>
</blockquote></div>