[Melbourne-pm] The || operator

David Dick david_dick at iprimus.com.au
Mon Oct 31 15:22:31 PST 2005


Alfie John wrote:
> Hi (),
> 
> Quick question:
> 
> @a = (4,5,6) || (7,8,9);
> use Data::Dumper;
> print Dumper[@a];
> 
> I would have thought @a would have contained (4,5,6). Looking through  
> perlop, it says that this is the wrong way to use || and what would  
> happen is:
> 
> @a = scalar( (4,5,6) ) || (7,8,9);
> 
> and thus @a would contain 3. But this is wrong too.
> 
> In fact, @a contains (6).
> 
> Any thoughts?

okay. guessing away here... :)

the (7,8,9) array ain't ever going to be reached, cos the || will short 
circuit it, so disregarding the (7,8,9) altogether and looking at 
perldoc -f scalar

                Because "scalar" is unary operator, if you acci­
                dentally use for EXPR a parenthesized list, this
                behaves as a scalar comma expression, evaluating
                all but the last element in void context and
                returning the final element evaluated in scalar
                context.  This is seldom what you want.

which would give you 6?

interestingly enuff, when you run the script under warnings, it warns 
twice of "Useless use of a constant in void context", which i s'pose 
refers to the '4' and '5'.





More information about the Melbourne-pm mailing list