develooper Front page | perl.perl5.porters | Postings from October 2013

Re: [perl #120202] Tie::Handle appends extra copies of $\ to output

Thread Previous
From:
Dave Mitchell
Date:
October 23, 2013 18:31
Subject:
Re: [perl #120202] Tie::Handle appends extra copies of $\ to output
Message ID:
20131023183050.GO2885@iabyn.com
On Sat, Oct 12, 2013 at 09:11:56PM -0700, Anno Siegel wrote:
> The following code demonstrates the problem:
> 
>     use Tie::Handle;
> 
>     my $out = do { no warnings 'once'; \ local *HANDLE };
>     tie *$out, 'Tie::StdHandle', '>&', \ *STDOUT or die;
> 
>     $\ = "haha\n";
>     print $out "hihi\n";
> 
> which prints
> 
>     hihi
>     haha
>     haha
> 
> The string in $\ has been added twice, once explicitly by
> Tie::Handle::PRINT and another time implicitly by the use of
> (CORE::) print in Tie::StdHandle::WRITE.
> 
> The bug also affects the use of say() with tied handles where a spurious
> newline is added by the same effect.
> 
> In my understanding, WRITE should never add anything to its output,
> so the fix could be simply
> 
> 
> --- lib/Tie/StdHandle_orig.pm	2013-10-13 04:04:53.000000000 +0200
> +++ lib/Tie/StdHandle.pm	2013-10-13 04:23:40.000000000 +0200
> @@ -64,6 +64,7 @@ sub GETC     { getc($_[0]) }
>  sub WRITE
>  {
>   my $fh = $_[0];
> + local $\;
>   print $fh substr($_[1],0,$_[2])
>  }

Thanks, I've applied this blead as

    2cf89ea7ef6ced6b38263ec224d4d1049bdf3cc0

along with some tests.

I also took the opportunity with some related commits to
move the test file over to Test::More, and fix an issue with WRITE
ignoring its offset argument.

-- 
"There's something wrong with our bloody ships today, Chatfield."
    -- Admiral Beatty at the Battle of Jutland, 31st May 1916.

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About