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

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;

Replies are listed 'Best First'.
Re^3: Conditional initialization of my-variables
by eyepopslikeamosquito (Archbishop) on Apr 07, 2023 at 23:32 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

Re^3: Conditional initialization of my-variables
by LanX (Saint) on Apr 07, 2023 at 19:40 UTC
    > 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.

        When dealing with empty strings, I'd rather check length

        Edit

        DB<13> p "false" unless "0" false DB<14> p "false" unless 0 false DB<15> p "false" unless length "0" DB<16> p "false" unless length undef false DB<17> use warnings; length undef DB<18> p "false" unless length "" false DB<19>

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