like already posted by pc88mxer you can set default values with the || operator which has the drawback that it not only triggers the default value when the variable is unset, i.e. undef, but also for all other boolean false values like: 0, "" and "0". Because your default value is 0 this doesn't matter because all of this values would be converted to 0 anyway, but a warning would be raised.

E.g. when the the function would be called as print_if(0) the given 0 would be overwritten by your default 0, which no consequences of course.
If your default value would be now 1 the call print_if(0) would be changed to print_if(1). To avoid this use:

$odVal = defined $odVal ? $odVal : 0; $osVal = defined $osVal ? $osVal : 0;
In Perl 5.10 there is now the // operator which does exactly this, so you could write it so:
$odVal //= 0; $osVal //= 0;
Then, of course, your code wouldn't run with Perl <5.10, so don't use this just now.

You also could change the code posted by jwkrahn from

my ( $FH, $orderKey, $odVal, $osVal ) = map $_ || 0, @_[ 0 .. 3 ];
to
my ( $FH, $orderKey, $odVal, $osVal ) = map defined $_ ? $_ : 0, @_[ 0 + .. 3 ];
Finally if you just like to remove the warning you could put a no warnings 'uninitialized'; in your function. Then undef values would be taken as 0 without a warning.

In reply to Re: default value for a scalar by mscharrer
in thread default value for a scalar by rightfield

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.