Warnings can be very helpful; I like them. But dealing with undefined values without triggering a warning can be a slight pain. While the following:
$dir= $ENV{TMPDIR} || "/tmp"; $file ||= "default";
work in many cases, they don't work when you need to distinguish certain false values from undefined. Now the following:
$size= defined $ENV{MAXSIZE} ? $ENV{MAXSIZE} : -1; $size= -1 if ! defined $size;
aren't so bad but require repeating either the variable name or the value. So def() gives you something that works very much like || and ||= but based on definedness, not falseness:
$size= def( $ENV{MAXSIZE}, -1 ); def( $size, -1 ); # void context causes $size to be modified if( 0 < def( $size= $ENV{MAXSIZE), -1, 1 ) ) # The third argument above forces $size to be modified # even though no void context used. An alternative is: if( 0 < ( $size= def $ENV{MAXSIZE), -1 ) )
sub def { my( $val, $alt, $force )= @_; $val= defined $alt ? $alt : "" if ! defined $val; $_[0]= $val if ! defined wantarray || $force; return $val; }

In reply to def -- Deal nicely with undefined values by tye

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.