From: "Geoffrey F. Green" <[EMAIL PROTECTED]>

> Here's a real beginners question:
> 
> What do the assignment operators "||=" and "&&="do?  I understand "+="
> and "-=" and the like;, but I can't figure out what these two do.
> 
> Perldoc perlop isn't much help, saying only that they "work as in C",
> and my copy of "The C Programming Language" -- don't ask why I have
> it, since I've never programmed in C -- doesn't shed any light on it.

All
        $var X= $something;
work like
        $var = $var X $something;

Therefore 
        $var ||= $something;
is basicaly the same as
        $var = $var || $something;

If you look into perlop at || you'll get :

        Binary "||" performs a short-circuit logical OR operation. That is,     
if the left operand is true, the right operand is not even evaluated. 
Scalar or list context propagates down to the right operand if it is 
evaluated.   

        The "||" and "&&" operators differ from C's in that, rather than 
returning 0 or 1, they return the last value evaluated. Thus, a 
reasonably portable way to find out the home directory (assuming it's 
not "0") might be:  

        $home = $ENV{'HOME'} || $ENV{'LOGDIR'} ||
            (getpwuid($<))[7] || die "You're homeless!\n";


This means that if you write
        $var = $one || $two;
then the $var will be set to $one (if $one's value of true) or to 
$two (otherwise).

And
        $var ||= $default;
will set the $var to the $default if it was set to a false value 
(undefined, empty string, zero or '0').

HTH, Jenda
===== [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
        -- Terry Pratchett in Sourcery


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to