develooper Front page | perl.fwp | Postings from December 2007

Re: bad code to good golf

Thread Previous | Thread Next
From:
shmem
Date:
December 13, 2007 14:41
Subject:
Re: bad code to good golf
Message ID:
Pine.LNX.4.64L.0712132335340.26576@elwood.think
Hello aristotle, hello all,

no good to hide away... *sigh*

From the keyboard of A. Pagaltzis [10.12.07,13:41]:

> * shmem <georg.moritz@millenux.com> [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. :-) )

Thank you ;-)

Had I tried to confuse you, I had added a smiley at the end, but
that has not been the case. See, that's my way to write bugs:
in gnawing on a solution, forgetting completely things that are
obvious to me in other circumstances.

Had somebody else posted that, I would have responded likewise,
but as every time, one is blind to his own bugs ;-)

0--gg-

> 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`.
> 
> 

-- 
_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s,/,($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e,e && print}

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About