nayakgi / perl-compiler

Automatically exported from code.google.com/p/perl-compiler
Other
0 stars 0 forks source link

Sub::Name::subname main::name with -O3 ( works with -O2 ) #345

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

> perlcc -O3 -r -e 'eval q/use Sub::Name; 1/ or die "no Sub::Name"; 
Sub::Name::subname("main::bar", sub { 42 } ); print "ok\n"; '

What is the expected output? What do you see instead?

whereas

> perlcc -O2 -r -e 'eval q/use Sub::Name; 1/ or die "no Sub::Name"; 
Sub::Name::subname("main::bar", sub { 42 } ); print "ok\n"; '
ok
> perl -e 'eval q/use Sub::Name; 1/ or die "no Sub::Name"; 
Sub::Name::subname("main::bar", sub { 42 } ); print "ok\n"; '
ok

Please use labels and text to provide additional information.

tested with release branch @07358def

Original issue reported on code.google.com by nicolas....@gmail.com on 5 Jun 2014 at 9:41

GoogleCodeExporter commented 9 years ago
So either compile Sub::Name (there's only the XS, so it comes with no cost), or 
use -O2 for this usecase.

Changing a static string is not allowed with -O3.
0x00007ffff632ffc2 in XS_Sub__Name_subname (cv=0xab3358) at Name.xs:67
67                      *end = 0;
(gdb) p end
$1 = 0x734884 <pv5+6> "bar"

Original comment by reini.urban on 6 Jun 2014 at 3:53

GoogleCodeExporter commented 9 years ago
perlcc -O3 -e'use Sub::Name;  Sub::Name::subname("main::bar", sub { 42 } ); 
print "ok\n"'

also does not work, so we have to patch Sub::Name.

Original comment by reini.urban on 6 Jun 2014 at 3:56

GoogleCodeExporter commented 9 years ago
Other possible workarounds are:

use a name without :: and '

perlcc -O3 -e'use Sub::Name;  subname("bar", sub { 42 } ); print "ok\n"'

or set the glob and be able to actually call it:

perlcc -O3 -e'$cv=sub{42}; *bar=*sub; print $bar->()'
perlcc -O3 -e'$cv=sub{42}; $bar=$sub; print $bar->()'

Original comment by reini.urban on 6 Jun 2014 at 4:08

GoogleCodeExporter commented 9 years ago
Or just use a variable instead of a constant string:

perlcc -O3 -e'use Sub::Name; $x="main::bar"; subname($x, sub { 42 } ); print 
"ok\n"'
ok

Original comment by reini.urban on 6 Jun 2014 at 4:10

GoogleCodeExporter commented 9 years ago
Fixed in the core testsuite with commit dd9169183e1c769d1de57c929b9183e23ecb9088
Author: Reini Urban <rurban@cpanel.net>
Date:   Wed Jul 2 12:20:05 2014 -0500

    t/CORE/mro/next_edgecases.t: fix wrong usage of constant string with Sub::Name

    Sub::Name changes the string argument, which will fail with constant strings
    in perlcc -O3 and embedders. Sub::Name really needs to return a copy then.
    See https://code.google.com/p/perl-compiler/issues/detail?id=345

Haven't filed a Sub::Name ticket yet

Original comment by reini.urban on 2 Jul 2014 at 5:21

GoogleCodeExporter commented 9 years ago
See https://rt.cpan.org/Ticket/Display.html?id=96893 for a pull request with 
Sub-Name-0.06

Original comment by reini.urban on 2 Jul 2014 at 8:56