On Thu, May 05, 2016 at 07:58:15PM +0200, Francesco Toscan wrote: > >Synopsis: Perl module OpenBSD::MkTemp. Unexpected behaviour in scalar > >context. <SNIP> > --- MkTemp.pm.orig Thu May 5 18:16:11 2016 > +++ MkTemp.pm Thu May 5 19:06:20 2016 > @@ -17,7 +17,7 @@ > { > my $template = shift; > my $fh = mkstemps_real($template, 0); > - return $fh && ($fh, $template) > + return wantarray() ? ($fh, $template) : $fh; > }
Thank you for the report. While this does correctly point out that the return value in scalar mode is incorrect and fixes calling in scalar context, it breaks error handling in list context as you can see if you run the test. This minor addition to the patch keeps the existing tests working and adds tests that scalar context returns a filehandle. OK? Index: cpan/OpenBSD-MkTemp//lib/OpenBSD/MkTemp.pm =================================================================== RCS file: /cvs/src/gnu/usr.bin/perl/cpan/OpenBSD-MkTemp/lib/OpenBSD/MkTemp.pm,v retrieving revision 1.4 diff -u -p -r1.4 MkTemp.pm --- cpan/OpenBSD-MkTemp//lib/OpenBSD/MkTemp.pm 25 Mar 2013 20:40:51 -0000 1.4 +++ cpan/OpenBSD-MkTemp//lib/OpenBSD/MkTemp.pm 8 May 2016 02:51:15 -0000 @@ -16,16 +16,16 @@ XSLoader::load('OpenBSD::MkTemp', $VERSI sub mkstemp($) { my $template = shift; - my $fh = mkstemps_real($template, 0); - return $fh && ($fh, $template) + my $fh = mkstemps_real($template, 0) || return; + return wantarray() ? ($fh, $template) : $fh; } sub mkstemps($$) { my($template, $suffix) = @_; $template .= $suffix; - my $fh = mkstemps_real($template, length($suffix)); - return $fh && ($fh, $template) + my $fh = mkstemps_real($template, length($suffix)) || return; + return wantarray() ? ($fh, $template) : $fh; } Index: cpan/OpenBSD-MkTemp//t/OpenBSD-MkTemp.t =================================================================== RCS file: /cvs/src/gnu/usr.bin/perl/cpan/OpenBSD-MkTemp/t/OpenBSD-MkTemp.t,v retrieving revision 1.4 diff -u -p -r1.4 OpenBSD-MkTemp.t --- cpan/OpenBSD-MkTemp//t/OpenBSD-MkTemp.t 25 Mar 2013 20:40:51 -0000 1.4 +++ cpan/OpenBSD-MkTemp//t/OpenBSD-MkTemp.t 8 May 2016 02:51:15 -0000 @@ -10,6 +10,7 @@ use warnings; use Test::More; use Errno; +use Scalar::Util qw( openhandle ); BEGIN { use_ok('OpenBSD::MkTemp') }; ######################### @@ -62,6 +63,17 @@ undef $fh2; open(F, ">$file2") || die "$0: unable to open $file2: $!"; cmp_ok(fileno(F), '==', $fileno, "mkstemp file handle ref counting"); +subtest "mkstemp in scalar context" => sub { + plan tests => 2; + ok my $fh = OpenBSD::MkTemp::mkstemp($template); + is openhandle($fh), $fh, "mkstemp returns a filehandle in scalar mode"; +}; + +subtest "mkstemps in scalar context" => sub { + plan tests => 2; + ok my $fh = OpenBSD::MkTemp::mkstemps($template, ".foo"); + is openhandle($fh), $fh, "mkstemps returns a filehandle in scalar mode"; +}; # # How about some failures?