perl メモ(三項演算子、短絡演算子)

#!/usr/bin/perl
use strict;
use warnings;
use 5.010;

{
  # 三項演算子
  my $true = !! 1;
  my $false = !! 0;
  my $result = $true ? "first" : "second";
  say $result; # first
}
{
  # 三項演算子の組み合わせ
  my $size = 15;
  my $result =
    ($size < 10) ? "small" :
    ($size < 20) ? "medium" :
    ($size < 30) ? "large" : "extra-large"; # デフォルト
  say $result; # medium
}
{
  # 短絡演算子
  my $u1 = undef;
  my $r1 = $u1 || "default";
  say $r1; # default

  my $u2 = undef;
  my $r2 = defined $u1 ? "defined" : "undefined";
  say $r2; # undefined
}
{
  # defined-or
  my $u1 = undef;
  my $r1 = $u1 // "default";
  say $r1; # default

  my $u2 = "defined";
  my $r2 = $u2 // "default";
  say $r2; # defined
}
{
  # 部分評価演算子
  my $m = 20;
  my $n = 10;
  ($m < $n) && ($m = $n);
  say "$m:$n"; # 20:10

  $m = 10;
  $n = 20;
  ($m < $n) && ($m = $n);
  say "$m:$n"; # 20:20

  ($m < 10) || say "Hello"; # Hello

  my $true = !! 1;
  my $false = !! 0;
  
  $false || say "false"; # false
  $true && say "true"; # true

  $false or say "die"; # die
  $true and say "die"; # die
}
exit;