Declarations with my and our are evaluated at compile time, but an if condition only at run-time.
All ways to execute the if at compile-time (like with BEGIN ) will introduce a surrounding block-scope again.
You could create a workaround with PadWalker, but still...
... the whole question is dubious, there is no point to conditionally declare a lexical variable, because you need to access it somewhere in your compiled code.
BUT the code won't compile if you access a lexical which wasn't declared before (at least under strict)
Maybe you should tell us what you really want to achieve... smells a lot like a XY Problem.
Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Wikisyntax for the Monastery
In reply to Re: how do i create a global var conditionally?
by LanX
in thread how do i create a global var conditionally?
by perltux
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |