in reply to Re: Which is your favourite cmt-deserving Perl feature?
in thread Which is your favourite cmt-deserving Perl feature?

jdhedden,
This idiom can bite viciously. This will assign the value of whatever() to $something if it has a false value. A false value doesn't necessarily mean undefined. Future Perl's will have a //= operator which means "defined or" but without a patch, if a defined but false value is valid - you need to write this more like $something = whatever() if ! defined $something

This is likely a good case to comment since when debugging having a reminder that defined false values may be overwritten.

Cheers - L~R

See What is truth? (Curiosity corner) for more details
  • Comment on Re^2: Which is your favourite cmt-deserving Perl feature?

Replies are listed 'Best First'.
Re^3: Which is your favourite cmt-deserving Perl feature?
by blazar (Canon) on Jul 22, 2005 at 10:17 UTC
    This is likely a good case to comment since when debugging having a reminder that defined false values may be overwritten.
    You're perfectly right. But, if one is aware of what he is doing ||= does its job most of the time. Of course we all will welcome //= to cover the remaining cases.

    Occasionally I use ||= on purpose to overwrite a false value (with another "standardized" false value), e.g.:

    $u ||= ''; # or $v ||= 0;
    (Most often this is not necessary either, and -as usual- there are other ways to do it...)