develooper Front page | perl.perl5.porters | Postings from January 2012

[perl #78980] Odd behavior when string filehandles and scalar assignment collide

Thread Next
From:
James E Keenan via RT
Date:
January 6, 2012 18:57
Subject:
[perl #78980] Odd behavior when string filehandles and scalar assignment collide
Message ID:
rt-3.6.HEAD-14510-1325905063-198.78980-15-0@perl.org
On Tue Nov 09 15:14:01 2010, comdog wrote:
> Cc: brian.d.foy@gmail.com
> Subject: Odd behavior when string filehandles and string operations
>    collide
> Message-Id: <5.13.4_1213_1289341116@mimibean.local>
> Reply-To: brian.d.foy@gmail.com
> To: perlbug@perl.org
> 
> 
> This is a bug report for perl from brian.d.foy@gmail.com,
> generated with the help of perlbug 1.39 running under perl 5.13.4.
> 
> 
> I don't know if this is a bug, but it is certainly an odd situation
> that wasted a day of my time debugging a filehandle to a string
> problem.
> 
> In a test file, I had opened a filehandle to a string and written some
> text to it. I then changed the scalar's value to clear it out (I
> thought), then reused the string filehandle thinking I'd get only the
> latest output in the scalar. That's not what happens, and I
>    understand,
> I think, why that's not what happens.
> 
> Here's a short example script. Although I've written this for 5.010
> and later, Perl 5.8 has the same problem:
> 
>     #!perl
>     use 5.010;
>     use strict;
>     use warnings;
> 
>     open my $string_fh, '>', \my $string;
>     print $string_fh "Buster likes liver treats";
>     show_string( $string );
> 
>     $string = '';
>     show_string( $string );
> 
>     print $string_fh "Mimi";
>     show_string( $string );
> 
>     sub show_string {
>         state $n = 0;
> 
>         printf "%d: string is [%s] length [%d]\n\thex [%s]\n",
>             $n++, $_[0], length $_[0],
>             join ":", map { sprintf '%02X', ord } split //, $_[0]
>             ;
>         }
> 
> Here's the output. (0) and (1) look fine, but it looks like perl is
> reusing the memory for the scalar, which has left over data from a
> previous state. It took me a long time to realize that there was a
> null byte at the beginning of the string for (2).
> 
>     0: string is [Buster likes liver treats] length [25]
>         hex
>   
[42:75:73:74:65:72:20:6C:69:6B:65:73:20:6C:69:76:65:72:20:74:72:65:61:74:73]
>     1: string is [] length [0]
>         hex []
>     2: string is [uster likes liver treatsMimi] length [29]
>         hex
>   
[00:75:73:74:65:72:20:6C:69:6B:65:73:20:6C:69:76:65:72:20:74:72:65:61:74:73:4D:69:6D:69]
> [snip]

Confirmed.  Do you know why that null byte is overwriting the 'B'?



---
via perlbug:  queue: perl5 status: new
https://rt.perl.org:443/rt3/Ticket/Display.html?id=78980

Thread Next


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