Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Bizarro %+ hash slice bug in Perl 5.10 to 5.14.

by tobyink (Canon)
on Jun 17, 2012 at 08:34 UTC ( [id://976633]=perlquestion: print w/replies, xml ) Need Help??

tobyink has asked for the wisdom of the Perl Monks concerning the following question:

The following code fails in Perl 5.10 to 5.14, but passes in 5.16.

use 5.010; use strict; use Test::More tests => 2; my $string = 'foo bar'; my $regexp = qr{^ (?<word1>\w+) \W+ (?<word2>\w+) $}x; sub get_words_1 { if ($string =~ $regexp) { my @tmp = @+{qw/ word1 word2 /}; return @tmp; } return; } sub get_words_2 { if ($string =~ $regexp) { return @+{qw/ word1 word2 /}; } return; } my @expected = qw(foo bar); my @results1 = get_words_1(); my @results2 = get_words_2(); is_deeply(\@results1, \@expected); is_deeply(\@results2, \@expected);

Is this apparent bug and bugfix documented anywhere? I can't find any reference to it in perl5160delta.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Replies are listed 'Best First'.
Re: Bizarro %+ hash slice bug in Perl 5.10 to 5.14.
by Anonymous Monk on Jun 17, 2012 at 09:23 UTC

      Thanks for looking into it. I had seen the UNIVERSAL::AUTOLOAD bug (it's the only mention of %+ in perl516delta), but dismissed it as I don't think Test::More installs a UNIVERSAL::AUTOLOAD, and I certainly wasn't. But you're right that the bug I was seeing could have been inadvertently fixed at the same time.

      I'll submit the test case to p5p in a couple of days unless anyone can convince me in this thread that the problem is already known to the core devs and tested for somewhere.

      This bug has already bitten one of my CPAN releases. Hurray to CPAN testers for alerting me to it so quickly!

      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: Bizarro %+ hash slice bug in Perl 5.10 to 5.14.
by dave_the_m (Monsignor) on Jun 17, 2012 at 16:58 UTC
    It was fixed by the following commit, which is a more general fix than just %+:
    commit 6f48390ab209d16ee8f795f0a83677c8bd9ac69c Author: Father Chrysostomos <sprout@cpan.org> Date: Wed Jan 4 23:28:54 2012 -0800 [perl #95548] Returned magical temps are not copied return and leavesub, for speed, were not copying temp variables wi +th a refcount of 1, which is fine as long as the fact that it was not c +op- ied is not observable. With magical variables, that *can* be observed, so we have to fore +go the optimisation and copy the variable if it’s magical. This obviously applies only to rvalue subs.

    Dave.

      Ah, this looks like it! The diff can be found here.

      The commit included a couple of tests, but I'll send p5p my test file too, just in case they want it.

      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://976633]
Approved by Old_Gray_Bear
Front-paged by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-23 22:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found