Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^3: Conditional initialization of my-variables

by eyepopslikeamosquito (Archbishop)
on Apr 07, 2023 at 23:32 UTC ( [id://11151524]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11151524]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2026-05-11 08:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.