in reply to Conditional initialization of my-variables
>
Yeah, that's a classic (and FAQ IIRC)
Short answer: my has also an runtime effect° to default the value to undef .
But that is bypassed here by the condition.
Do this instead:
my $var; $var = "<value>" if <condition>;
°) "undocumented implementation detail" may be more precise
See also
NOTE: The behaviour of a my, state, or our modified with a statement modifier conditional or loop construct (for example, my $x if ...) is undefined. The value of the my variable may be undef, any previously assigned value, or possibly anything else. Don't rely on it. Future versions of perl might do something different from the version of perl you try it out on. Here be dragons.
Cheers Rolf
(addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
Wikisyntax for the Monastery
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Conditional initialization of my-variables (runtime)
by ikegami (Patriarch) on May 11, 2023 at 08:31 UTC |