in reply to Temporary Variables
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.
|
|---|