develooper Front page | perl.perl5.porters | Postings from November 2014

str overload

Thread Next
From:
Paul Johnson
Date:
November 30, 2014 16:03
Subject:
str overload
Message ID:
20141130160250.GC31019@pjcj.net
This came up when I was testing Devel::Cover with recent releases:

$ cat overloaded
my $x = 0;

package two_face;

sub new { my $p = shift; bless [@_], $p }
use overload '""' => \&str, '0+' => \&num, fallback => 1;
sub num { shift->[1] }
sub str { $x++; shift->[0] }

package main;

my $seven = new two_face ("vii", 7);
printf "seven = %d\n", $seven;
print "x = $x\n";

$ perl5.21.4 overloaded
seven = 7
x = 0
$ perl5.21.5 overloaded
seven = 7
x = 1
$


That example code is basically copied from the documentation of
overload.pm.  As you can see, we are now calling str() where we used not
to.  This was introduced with the following commit:

commit 354b74ae6f54fdd18bcf40fc3afcdaddc8b2ca9d
Author: Father Chrysostomos <sprout@cpan.org>
Date:   Sat Sep 27 06:48:04 2014 -0700

    [perl #12285] Fix str vs num inf/nan treatment
    
    sprintf, pack and chr were treating 0+"Inf" and "Inf" differently,
    even though they have the same string and numeric values.
    
    pack was also croaking for 0+"Inf" passed to a string format.


The question is whether whether or not this is acceptable.

-- 
Paul Johnson - paul@pjcj.net
http://www.pjcj.net

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