Re: bad code to good golf
"A. Pagaltzis" <[email protected]> Mon, 10 Dec 2007 13:41:46 +0100
| Newsgroups | gmane.comp.lang.perl.fun |
|---|---|
| Message-ID | <20071210124146.GB26886@klangraum> |
* shmem <[email protected]> [2007-12-10 12:55]: > Well, if we were to return an array reference instead of a hash > ref, > > sub new{bless\@_,shift} > > works. Why does the shift get executed before an array > reference is constructed - but not if a hashref is constructed > - from an array? It probably doesn’t. Either way, evaluation order is a red herring: your construction will work regardless of which subexpression is evaluated first! You managed to confuse yourself. Congratulations. :-) (Or you are underhandedly trying to confuse us. In that case, sorry bub, better luck next time. :-) ) What happens is that `\` takes a *reference* to the array. And of course when you do that, any modifications of the referent, including *subsequent* modifications, will be seen by anyone who holds a reference to it. In contrast, the hash constructors make a *copy* of the array, and that copy will not be affected but subsequent modifications to the source array. Anyone who has trouble following should consider the difference between the following two: sub new { bless \@_, shift } # vs sub new { bless [@_], shift } Consider how many arrays are involved in either case, and which one is affected by the `shift`. -- *AUTOLOAD=*_;sub _{s/(.*)::(.*)/print$2,(",$\/"," ")[defined wantarray]/e;$1} &Just->another->Perl->hack; #Aristotle Pagaltzis // <http://plasmasturm.org/>