in reply to Conditional initialization of my-variables

Your example has reinforced my long-held support for Use block if not postfix if (as recommended in Perl Best Practices).

Though a bit longer, I've always found block if to be clearer and easier to understand. The big win comes during code review whenever you want to add an extra statement to the if condition -- that's a simple one line change to a block if, compared to a more violent restructuring of the code from postfix if to block if.

See also the first principle from Re: Big cache (my top ten software development practices):

and my longer Coding Standards Links.

Replies are listed 'Best First'.
Re^2: Conditional initialization of my-variables
by Bod (Parson) on Apr 07, 2023 at 19:26 UTC
    Your example has reinforced my long-held support for Use block if not postfix if

    Having struggled to understand code that I wrote just a few years ago that used postfix if conditions, I concluded that if I can't understand it quickly, anyone else would have great difficulty simply because they didn't write it. So now I almost exclusively use conditional blocks.

    The one major exception is where the condition modifies the condition variable:

    my $input = <STDIN>; $input = 10 if $input > 10;
    or to add a default value
    $input = 1 unless $input;

        > 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

      > or to add a default value

      > $input = 1 unless $input;

      Careful! Many things are false in Perl, like 0 or ""

      You probably meant

      $input = 1 unless defined $input;

      Anyway, both are IMHO better written as

      $input ||= 1; # default if false # or $input //= 1; # default if undefined

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

        Careful! Many things are false in Perl, like 0 or ""

        Oh yes - care is needed. But very often that's exactly what I want.

        A common use case is query strings from HTML forms with checkboxes. The query string will have the key with no value. So if that's decoded into a Perl hash, the hash will be defined and the key exists but I want it to return false as it is an empty string.