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

Re: [perl #114410] Substr giving wrong results on $1 with utf8

Thread Previous | Thread Next
From:
Nicholas Clark
Date:
August 31, 2012 02:27
Subject:
Re: [perl #114410] Substr giving wrong results on $1 with utf8
Message ID:
20120831092730.GB9583@plum.flirble.org
On Thu, Aug 30, 2012 at 06:20:13PM -0700, Father Chrysostomos via RT wrote:
> I've fixed this in commit 7d1328bb7c by reset utf8 caches in mg_get

This seems like a sensible solution. (I can't spot any flaws in the approach)


Historically, if something breaks because of tie, it usually also breaks
with overloading:

$ cat 114410.pl
#!/perl -w
use strict;

package UTF8Toggle;
use strict;

use overload '""' => 'stringify', fallback => 1;

sub new {
    my $class = shift;
    my $value = shift;
    my $state = shift||0;
    return bless [$value, $state], $class;
}

sub stringify {
    my $self = shift;
    $self->[1] = ! $self->[1];
    if ($self->[1]) {
	utf8::downgrade($self->[0]);
    } else {
	utf8::upgrade($self->[0]);
    }
    $self->[0];
}

package main;

my $u = UTF8Toggle->new(" \x{c2}7 ");

printf "%d\n", ord substr $u, 1;
printf "%d\n", ord substr $u, 1;

__END__
$ ./perl -Ilib 114410.pl     
194
panic: sv_pos_u2b_cache cache 5 real 4 for  Â7  at 114410.pl line 32.


I'm not sure what the best fix is here. Given that I'd been wondering if the
fix for #114410 was to outlaw caching of tied values, but simply expiring
the cache on the next read works, is the right fix here to trap all points
that call into overload value returning routines and reset the cache?

Nicholas Clark

Thread Previous | 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