in reply to Re: Combining hashes of hahses?
in thread Combining hashes of hahses?

Grandfather, What is the purpose of
$dnaLen ||= length $2;
first i was just reading to see if I can make sense of the notation of ||= , but I found out that it's just $dnaLen = $dnaLen || length $2; But in this case, $dnaLen would never be anything other than 0(false).. ? am I not reading this correctly?

UPDATE -- I guess it's being used here
die "No dna data found" unless $dnaLen; die "No morph data found" unless $morphLen;

Replies are listed 'Best First'.
Re^3: Combining hashes of hahses?
by GrandFather (Saint) on Nov 07, 2007 at 22:09 UTC

    $x ||= something; is commonly used to give $x a value if it hasn't one already (more correctly, if the current value is false). In the case cited it is to pick up the first non-zero length of a dna string. There is an implicit assumption that all dna strings are the same length.

    Note that Perl returns the value of which ever true value it finds when evaluating || (not simply a true or false value) so $x gets the value 'something' regardless of what the nature of 'something' is if $x is false to start with. In particular, this trick can be used to set a scalar to a default string if the scalar hasn't been set already:

    my $error; ... $error ||= 'No error found';

    Perl is environmentally friendly - it saves trees
      thank you always