in reply to Re: Logic for importing and strict vars?
in thread Logic for importing and strict vars?

It has something to do with BEGIN

I have found this information:

BEGIN: A BEGIN code block is executed as soon as possible, that is, the moment it is completely defined, even before the rest of the containing file (or string) is parsed

So I suspect that that means that in case the BEGIN is not there the parser will detect the variable $x before it is created. In case the BEGIN is there, it is guarantied to execute that block first, so before it sees $x (or in this case actually $main::x) and by that time it exists because the block has already been executed.

edit: It even looks like that BEGIN turns off certain warnings for this reason:

perl -MO=Deparse -wMstrict -e "{ package bla { *::x=\$a } }" Name "main::x" used only once: possible typo at -e line 1. ...
perl -MO=Deparse -wMstrict -e "BEGIN { package bla { *::x=\$a } }" # No problem

Replies are listed 'Best First'.
Re^3: Logic for importing and strict vars?
by haukex (Archbishop) on Feb 28, 2019 at 08:49 UTC
    It has something to do with BEGIN
    In case the BEGIN is there, it is guarantied to execute that block first, so before it sees $x (or in this case actually $main::x) and by that time it exists because the block has already been executed.

    Yes, I concur - and an interesting point regarding the warnings. I assume they don't occur because otherwise they'd get triggered by the import mechanism regularly, but it'd be interesting to find any docs on that, too...