in reply to Logic for importing and strict vars?

To add a little strangeness; for your third proposition, on v5.10 Linux I get:

Name "blah::a" used only once: possible typo at -e line 1.
but I don't on v5.26 Windows :)

All I can say is that with B::Deparse the package prefix seems to be removed,

perl -MO=Deparse -wMstrict -e "BEGIN { *::x=\$a } $x++" ... BEGIN { $^W = 1; } use strict; sub BEGIN { *x = \$a; } $main::x++;
The output is exactly the same if I add package main; before setting *::x. (Also it replaces the $x++ by $main::x++, which would actually be valid code...) This might just be an optimization gone wrong?

Replies are listed 'Best First'.
Re: Logic for importing and strict vars?
by Veltro (Hermit) on Feb 27, 2019 at 11:16 UTC
    Eily: All I can say is that with B::Deparse the package prefix seems to be removed,

    No, that is turned around reasoning. I would say: The package prefix is not added.

    The problem that is described here is not in the BEGIN block but the last $x++ statement which should be explicitly mentioned: $main::x++; because $x is not exported imported. The Parser translates to '$main::x' automatically because the package statement resides in the BEGIN block. When it is outside the BEGIN block, it doesn't do that. In none of the cases, the variable $x is exported imported.

    edit: As far as documentation concerned I believe it can be found under: package, paragraph 2, the promise that a package statement does not affect lexically-scoped variables

      The prefix was there, and then it's not. I'm not sure why I couldn't call that being removed.

      In none of the cases, the variable $x is imported
      Importing is just writing in the glob slot from another package, which is what Exporter does (and it doesn't do anything else, the only special thing is that it is called implicitly, but you can call it explicitly and still have it work). The part that I missed on first read was the "from another package". Which means the variable is imported in the third case (edit: which is why the third one works).

Re^2: Logic for importing and strict vars?
by haukex (Archbishop) on Feb 28, 2019 at 08:37 UTC
    on v5.10 Linux I get: Name "blah::a" used only once: possible typo at -e line 1. but I don't on v5.26 Windows :)

    That'd be this change in v5.20: $a and $b warnings exemption

    All I can say is that with B::Deparse the package prefix seems to be removed

    True, although that's with the error still happening, I'm not sure if the deparse can be trusted in that case. It looks a little different without strict vars:

    $ perl -MO=Deparse -wMstrict=refs,subs -e 'BEGIN { *::x=\$a } $x++' BEGIN { $^W = 1; } use strict 'refs', 'subs'; sub BEGIN { *x = \$a; } ++$x; -e syntax OK