in reply to default values

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

Replies are listed 'Best First'.
Re: Re: default values
by wolis (Scribe) on Sep 16, 2003 at 07:14 UTC
    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

        Good point however I was more asking the question of:
        "Does it make any difference speed wise or logic wise to ask if a match is true or a match is false in an if statement?"

        I dont think I have ever had an issue with the speed at which Perl does things. Blindingly fast is good enough for me :-)

        I like giving my variables nice descriptive names and making my if elses open and legible - especially if I expect someone else to pick up the code and do something sensible with it :-)

        I guess I'm remembering the old BASIC days when CPUs were slow and it mattered how things were done.

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