-
Notifications
You must be signed in to change notification settings - Fork 560
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BEGIN blocks have wrong caller package #15597
Comments
From @maukeCreated by @mauke#!perl package Mtfnpy; __END__ Output: Expected output: Perl Info
|
From @cpansproutOn Sat Sep 10 02:13:50 2016, mauke- wrote:
I’m not entirely sure I would expect that. I’m not sure what to expect. After all, ‘calling’ BEGIN in this case has not started running yet. -- Father Chrysostomos |
The RT System itself - Status changed from 'new' to 'open' |
From @maukeOn Sat Sep 10 07:31:47 2016, sprout wrote:
I don't understand your last sentence but the reported location of the call (which is the "}" of the BEGIN block) is in package Mtfnpy, not main. At minimum it's inconsistent. |
From @iabynOn Sat, Sep 10, 2016 at 07:47:42AM -0700, l.mai@web.de via RT wrote:
What's happening is as follows: At compile time, 'package Foo' sets PL_curstash to point to %Foo::. At run time, each nextstate op that is executed, sets PL_curcop to point When a scope (such as a sub) is entered, the current value of PL_curcop caller() uses the saved PL_curcop's on the context stack to extract out When BEGIN is called we're not actually in runtime, so PL_curcop points to PL_compiling gets its cop_line etc fields updated every time the toker -- |
From @cpansproutOn Mon Sep 12 03:57:32 2016, davem wrote:
PL_curstash is used at compile time instead of CopSTASH(&PL_compiling). I have always assumed it was for efficiency’s sake. But, come to think of it, if PL_compiling is a fixed buffer in the interpreter struct, then #defining PL_curstash to CopSTASH(&PL_compiling) would make no speed difference on threaded builds, but I don’t know about non-threaded. Then again, I may be wrong about the reason why PL_curstash is separate and CopSTASH(&PL_compiling) is unused. Hmm, maybe it’s because cops do not contain stash pointers under threads. Maybe the solution is to have pp_caller check whether its cop == &PL_compiling and use PL_curstash instead. -- Father Chrysostomos |
From @maukeOn Mon Sep 12 03:57:32 2016, davem wrote:
Thanks for this detailed explanation! I discovered this issue because I have a C module that calls a Perl sub at parse time. That Perl sub always saw the wrong calling package and I didn't understand why. So I thought I'd check how perl itself handles that situation in BEGIN blocks (and maybe steal the code). As it turned out, BEGIN blocks have exactly the same issue. Based on your explanation I've come up with the following workaround: assert(PL_curcop == &PL_compiling); It seems to pass all tests. :-) (I don't know what the best way is to fix this in core.) |
From @maukeOn Sat Sep 10 02:13:50 2016, mauke- wrote:
Here's a patch that adds a TODO test for now. |
From @mauke0001-t-op-caller.t-add-a-TODO-test-for-RT-129239.patchFrom bb3329a391fce27c00dd55aeaf5357337aa09192 Mon Sep 17 00:00:00 2001
From: Lukas Mai <l.mai@web.de>
Date: Fri, 21 Oct 2016 15:18:38 +0200
Subject: [PATCH] t/op/caller.t: add a TODO test for RT #129239
---
t/op/caller.t | 16 +++++++++++++++-
1 file changed, 15 insertions(+), 1 deletion(-)
diff --git a/t/op/caller.t b/t/op/caller.t
index 3017465..1ffb5b3 100644
--- a/t/op/caller.t
+++ b/t/op/caller.t
@@ -5,7 +5,7 @@ BEGIN {
chdir 't' if -d 't';
require './test.pl';
set_up_inc('../lib');
- plan( tests => 97 ); # some tests are run in a BEGIN block
+ plan( tests => 100 ); # some tests are run in a BEGIN block
}
my @c;
@@ -352,3 +352,17 @@ EOP
$::testing_caller = 1;
do './op/caller.pl' or die $@;
+
+{
+ package RT129239;
+ BEGIN {
+ my ($pkg, $file, $line) = caller;
+ ::is $file, 'virtually/op/caller.t', "BEGIN block sees correct caller filename";
+ ::is $line, 12345, "BEGIN block sees correct caller line";
+ TODO: {
+ local $::TODO = "BEGIN blocks have wrong caller package [perl #129239]";
+ ::is $pkg, 'RT129239', "BEGIN block sees correct caller package";
+ }
+#line 12345 "virtually/op/caller.t"
+ }
+}
--
2.10.0
|
From @KES777This issue seems is related: https://rt.perl.org/Public/Bug/Display.html?id=127083 |
This is a duplicate of #15109. |
Migrated from rt.perl.org#129239 (status was 'open')
Searchable as RT129239$
The text was updated successfully, but these errors were encountered: