in reply to Re: Swapping two values
in thread Swapping two values
As I've pointed out before, using XORs to swap values in perl will not work with references. (Note, by the way, that the OP was an inquiry about swapping references.)
References aren't the only values this naive method will break on. Try it with $a = 1; $b = "0foo"; for instance.
In C or assembly, you can swap values with bitwise XOR to avoid the use of temporary storage. It's rarely necessary, but comes in handy when you need things to be real fast and you have a limited number of registers. In such lower level languages, it works on strings because strings are pointers and pointers are ints and XOR works nicely on ints. In perl, however, the XOR method will do a bitwise XOR on the whole string. When all you want to do is swap values, that's grossly inefficient.
Bitwise XOR should never be used to swap values in perl. It isn't maintainable. It isn't efficient. It isn't clever. Don't do it.
-sauoq "My two cents aren't worth a dime.";
|
|---|