in reply to Temporary Variables

"Other languages force you to use a temporary variable to swap variables, but perl doesn't. In perl you can do this: ($a,$b)=($b,$a)." srawls points out that TIMTOWTDI

Yes, it is absolutely true that it is possible to use XOR in order to swap the values of two variables without the use of a temporary variable*. This fact is fairly well-known, and I'd be very surprised of the authors of Programming Perl didn't know about it. I assume that they were just trying to highlight the fact that Perl provides an easy and obvious way to swap the values of 2 variables. Perl is a higher-level language, which means that it provides a higher-level of abstraction. For a number of reasons, higher-level language programmers don't usually want to use 3 rather obscure-appearing statements when they could use one quite obvious self-documenting one to directly accomplish the same thing. It is the job of the interpreter to substitute optimal lower-level code for these powerful, tidy constructions.


*Not every language provides XOR so the statement in the book isn't entirely incorrect. Putting a "Some" in front of that sentence gets them right off the hook entirely, assuming that it isn't there already - I haven't got the time to try to find it.