in reply to Re^2: Conditional initialization of my-variables
in thread Conditional initialization of my-variables

Bod, I suggest you consider dropping your "major exception" in favour of LanX's excellent advice. That is, I prefer LanX's:

$input //= 1; # default if undefined
to your:
$input = 1 unless defined $input;

See also:

Replies are listed 'Best First'.
Re^4: Conditional initialization of my-variables
by LanX (Saint) on Apr 08, 2023 at 12:57 UTC
    > How to set default values in Perl

    Gabor is mixing two cases in his article,

    • defaulting undef values
    • defaulting missing values (like with the hashes)
    But doesn't give an example for missing values in arrays.

    TIMTOWTDI, alas not very DRY

    DB<30> x @_ 0 1 1 2 DB<31> x ($a,$b,$c,$d) = (@_, ("D1".."D4")[@_..3]) 0 1 1 2 2 'D3' 3 'D4' DB<32>

    or

    DB<32> @DEF= ("D1".."D4") DB<33> x ($a,$b,$c,$d) = (@_, @DEF[@_..$#DEF]) 0 1 1 2 2 'D3' 3 'D4' DB<34>

    edit

    or

    use v5.12.0; use warnings; use Data::Dump qw/pp dd/; sub test { my ($x, $y, $z) = ( @_, ("X","Y","Z")[@_..42] ); pp ($x, $y, $z); } test(1..$_) for 0..3
    -->
    ("X", "Y", "Z") (1, "Y", "Z") (1, 2, "Z") (1, 2, 3)

    Cheers Rolf
    (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
    Wikisyntax for the Monastery