in reply to Conditional initialization of my-variables

Hi

>

my $var = "<value>" if <condition>;

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>;

update

°) "undocumented implementation detail" may be more precise

See also

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

    Alternatively,

    my $var = <condition> ? "<value>" : undef;