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

Hi Monks

Just a tiny question on the use of:

$val = ($val ne '') ? $val : $default_val;
Is there a simpler way to just say "if val is blank then set it to a default value" without having to refer to the $val three times in the one line?

I know I can write a function (do we call them functions even tho the are 'Sub's?) - oops thats a second question! - but I was wondering what secrets Perl has that might do this.

Thanks,

___ /\__\ "What is the world coming to?" \/__/ www.wolispace.com

Replies are listed 'Best First'.
Re: default values
by Limbic~Region (Chancellor) on Sep 16, 2003 at 01:59 UTC
    wolis,
    It depends.
    $val ||= $default_val;
    This says if $val has any value that isn't false, leave it alone - else set it to $default_val. The problem arises if $val evaluates to false (0, "", "0", undef). If you don't care about those cases, you can shorten it.

    Cheers - L~R

      Interesting.. I wonder if I do care about the different values evaluating to false.

      Would there be a natty way fo using the $_ (or whatever returns the last variable used?)

      Shot in the dark:

      $val = ($_ eq "") ? $default_val : $_;
      As you can probably see I dont have a full grasp of this concept :-)

      While we are on this topic.. do you know if there is any speed (trivial tho it may be) or logical difference in checking for a value being equal or not equal first?

      $a = ($b ne $c) ? $d : $e; $a = ($b eq $c) ? $e : $d;

      ___ /\__\ "What is the world coming to?" \/__/ www.wolispace.com
        wolis,
        I think you are under the misconception that more verbose code automatically means slower code. I doubt seriously that you would be able to notice a significant measurable performance advantage here without millions of calculations. I would take a look at why premature optimization can be a bad thing.

        Typically, performance issues are caused by inefficient algorithms as a whole and not individual lines of code. There are cases where an individual line in a tightly wrapped loop can be a problem. Then there are cases where people try to use Perl in a situation better suited for C, not because Perl is incapable of getting the job done - but because performance requirements dictate it.

        Cheers - L~R

Re: default values
by stajich (Chaplain) on Sep 16, 2003 at 02:05 UTC
    Assuming the 0 is not one of the possible values
    $val ||= $default_val;
    If 0 could be proper value (and isn't the default, otherwise the above would still work)
    $val = $default_val unless defined $val;
    Dealing with possible undefs and 0 as the state of $val will require it 3 times I think
    $val = $default_val unless defined $val && length($val);
    I suspect others might have shorter solns... FORE!

        I tried this but it did not work. ;-)

        cheers

        tachyon

        s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: default values
by flounder99 (Friar) on Sep 16, 2003 at 11:52 UTC
    I think something like:
    $val = $default_val if $val eq '';
    is easier to read and has the same effect as your code.
    $val ||= $default_val;
    Does not have the same effect as your code. It will set the default value if $val is false but not necessarily the empty string.
    $default_val = "default"; $val = '0'; $val ||= $default_val; print $val;
    will print default even though $val is not and empty string;

    --

    flounder

Re: default values
by mpd (Monk) on Sep 16, 2003 at 02:03 UTC
    Would $val = $val || $default_val; work for you?

    Edit: Bah. Beaten to the punch :p and L~R's answer is more succinct anyway.