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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.