Hi sk,
The ||= operator will assign its right side to its left side if the left side is a "false" value (0, "0", "" and undef, as per Truth and Falsehood).
use Data::Dumper; $Data::Dumper::Terse=1; $Data::Dumper::Useqq=1; $Data::Dumper::Indent=0; for my $x (0, "0", "", undef, "true", 1) { my $y=$x; $y||='?'; print Dumper($x), " -> ", Dumper($y), "\n"; } __END__ 0 -> "?" 0 -> "?" "" -> "?" undef -> "?" "true" -> "true" 1 -> 1
Other than that, if you want to specifically test for the empty string, your best bet is probably length, as Your Mother showed. As of Perl v5.12, when you give it an undef input, it returns undef instead of issuing a warning, so you can just use length in your boolean test without worrying about definedness.
Hope this helps,
-- Hauke D
Update 2019-08-17: Updated the link to "Truth and Falsehood".
In reply to Re: searching for clear ways to overwrite the empty string
by haukex
in thread searching for clear ways to overwrite the empty string
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |