defined-or(//)
ããã°ãç¶ããªããã | perl ã®ä½æ³ #2 ã||=ãã®ä½¿ãæ¹ ããå¼ç¨
perl ã®ä½æ³ #2 ã||=ãã®ä½¿ãæ¹
ããã«ãããã¯
$value ||= $default_value;
ã¨æ¸ãã¾ãã
â»æ³¨æ
$value ã«ã0ããã»ããããå¯è½æ§ãããå ´åã¯ãã®æ¸ãæ¹ã¯ã§ãã¾ãããã0ããå½ã¨è©ä¾¡ããã¦ãã¾ãããã§ãããã®å ´ådefined ãªã©ãå©ç¨ãã¾ãããã
ã¨ãããã¨ã§ããã||= ã ã¨ç©ºæååãå½ã¨è©ä¾¡ããã¾ããundef ã®æã ãå¤ãå¤æ´ããããªã //= ã使ãã®ãæ£è§£ãã¨ã
#!/usr/local/bin/perl use strict; use warnings; print "logical or (||=)\n"; printf "string = %s\n", logical_or("string"); printf "1 = %s\n", logical_or(1); printf "undef = %s\n", logical_or(undef); printf "'' = %s\n", logical_or(''); printf "0 = %s\n", logical_or(0); print "-" x 40, "\n"; print "logical defined or (//=)\n"; printf "string = %s\n", logical_defined_or("string"); printf "1 = %s\n", logical_defined_or(1); printf "undef = %s\n", logical_defined_or(undef); printf "'' = %s\n", logical_defined_or(''); printf "0 = %s\n", logical_defined_or(0); sub logical_defined_or { my $value = shift; $value //= "CHANGED"; return $value; } sub logical_or { my $value = shift; $value ||= "CHANGED"; return $value; }
ãããªã¹ã¯ãªãããæ¸ãã¦å®è¡ãã¦ã¿ã¾ãã
logical or (||=) string = string 1 = 1 undef = CHANGED '' = CHANGED 0 = CHANGED ---------------------------------------- logical defined or (//=) string = string 1 = 1 undef = CHANGED '' = 0 = 0
//, //= ã使ããã®ã¯ Perl 5.8.1 ããã®ããã§ããperldelta581 ã«ãããªè¨è¿°ãããã¾ããã
o A new operator "//" (defined-or) will be available. This means that one will be able to say $a // $b instead of defined $a ? $a : $b and $c //= $d; instead of $c = $d unless defined $c; The operator will have the same precedence and associativity as "||". A source code patch against the Perl 5.8.1 sources will be available in CPAN as authors/id/H/HM/HMBRAND/dor-5.8.1.diff.
ã¨ããã§ãã® defined-or(//) æ¼ç®åã£ã¦ experimental ãªãã®ã§ã¯ãªããã§ãããï¼ã©ããã§ãããªè¨è¿°ãè¦ããããªæ°ãããã®ã§ãããæãåºãã¾ããããã¼ã¯ã¼ã "//" ã ã¨ãã¾ããã¨æ¤ç´¢ã§ããªãã¦ãããperlop è¦ã¦ãç¹ã«ä½ãæ¸ãã¦ãªãããåé¡ãªãã®ããªï¼èª°ãç¥ã£ã¦ããæãã¦ãã ããã