rightfield has asked for the wisdom of the Perl Monks concerning the following question:

Hi experts, I have two values that I am passing to a subroutine. If they are empty I want to give them a default value of zero. How should I change what I am doing because I keep getting the complaint: "Use of uninitialized value in length..." What I am trying to accomplish by the subroutine is to only print the line if there is a value greater than zero in either of the scalars. This how I call the subroutine:
print_if(\*FH,'AXS ',$aOrder->{rx_OD_Axis},$aOrder->{rx_OS_Axis});
This is the subroutine:
sub print_if { my($FH, $orderKey, $odVal, $osVal) = @_; #if (!defined $odVal) {$odVal = 0;} #if (!defined $osVal) {$osVal = 0;} if (!(length($odVal) > 0)) {$odVal = "0";} if (!(length($osVal) > 0)) {$osVal = '0';} if ($odVal > 0 || $osVal > 0) { print $FH $orderKey.' '.$odVal.' '.$osVal."\n"; } }
Thank you for your help. Warm regards,

Replies are listed 'Best First'.
Re: default value for a scalar
by pc88mxer (Vicar) on Apr 19, 2008 at 20:56 UTC
    I understand your issue now... the arguments to $osVal and $odVal can either be undef or the empty string when the default value should be used for them.

    In that case, a convenient solution is the ||= operator:

    $odVal ||= 0; $osVal ||= 0;
      Thank you pc88mxer your solution worked first time. Warm regards.
Re: default value for a scalar
by jwkrahn (Abbot) on Apr 19, 2008 at 21:20 UTC

    Change:

    my ( $FH, $orderKey, $odVal, $osVal ) = @_;

    To:

    my ( $FH, $orderKey, $odVal, $osVal ) = map $_ || 0, @_[ 0 .. 3 ];

      Thank you jwkrahn your solution worked as well as the first and I learned a new function. Warm regards
Re: default value for a scalar
by mscharrer (Hermit) on Apr 20, 2008 at 10:22 UTC
    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.