By "not documented', I was refering to what is supposed to be returned by sort. To a Perl programer, should what's returned by sort be considered a 'true' copy of a original list (albeit sorted) which can be worked on without affecting the original list under any circumstances? As far as I see it, in the code of 'foreach (sort @array){}', foreach is stepping through a sorted copy returned by sort at the time sort has already finished and @array forgotten. So I wouldn't expect @array to be changed. The current behavior is a bad behavior to say the least. I would be very surprised if Larry would design the code to behave in such a way. ;-)
> From [EMAIL PROTECTED] Fri Jul 22 09:07:30 2005 > Subject: Re: [perl #36616] bug or feature? foreach (sort @array) > {y/a-z/A-Z/;} # @array modified! > From: "Scott R. Godin via RT" <[EMAIL PROTECTED]> > X-RT-Loop-Prevention: perl > RT-Ticket: perl #36616 > Managed-by: RT 3.0.11 (http://www.bestpractical.com/rt/) > RT-Originator: [EMAIL PROTECTED] > To: [EMAIL PROTECTED] > X-RT-Original-Encoding: utf-8 > Date: Fri, 22 Jul 2005 06:05:23 -0700 > MIME-Version: 1.0 > Content-Transfer-Encoding: 7bit > > Shiping Zhang wrote: > > >>The described behavior changes whether "sort @arr" goes directly in > >>'for' or you assign it first to an array variable. > >> > >> @arr = ('one', 'two', 'three', 'four'); > >> y/a-z/A-Z/ foreach sort @arr; > >> print "\na: @arr\n" > >> > >>prints "a: ONE TWO THREE FOUR" and > >> > >> @arr = ('one', 'two', 'three', 'four'); > >> @s = sort @arr; > >> y/a-z/A-Z/ foreach @s; > >> print "\na: @arr\n" > >> > >>prints "a: one two three four". This has to do with copy-on-assign? > >> > >>That seems like a (subtle) feature. Otherwise, it is a very old bug: > >>the same behavior with 5.8.6, 5.6.1, and 5.005_03. > >> > > > If it's a feature, then it's not documented at all. Larry's Perl books do > > not > > mention it, the sort man page from perldoc does not mention it, and the > > manual > > page from www.perl.org does not mention it. And this feature is against > > intuition and I would consider it more a bug than a feature (what use it > > has?). > > I think other people wouldn't expect @array to be changed in the code above. > > (New Perl programers may be surprised to know that foreach works on an > > alias, > > but it's well documented (and warned) and such feature is at least useful > > under > > some circumstances.) Conceptually, sort should return a true copy (sorted > > of > > course) of its arguement and I think it is how people would understand it. > > (How sort is implemented is a different matter. I guess sort is working on a > > list of pointers to the elements of the original list and, in foreach > > context, > > just returns a pointer to the list of pointers instead of making an > > anonymous > > copy of the sort list.) > > > > it's documented under perldoc perltrap as a discontinuance between perl4 > and perl5 so it's been around for quite a while. > > it's also documented under perldoc perlsyn: > > "If any element of LIST is an lvalue, you can modify it by modifying VAR > inside the loop. Conversely, if any element of LIST is NOT an lvalue, > any attempt to modify that element will fail. In other words, the > "foreach" loop index variable is an implicit alias for each item in the > list that you're looping over." > > it's ALSO mentioned in perlfaq4: perldoc -q "strip blank space" > This communication is for use by the intended recipient and contains information that may be privileged, confidential or copyrighted under applicable law. If you are not the intended recipient, you are hereby formally notified that any use, copying or distribution of this e-mail, in whole or in part, is strictly prohibited. Please notify the sender by return e-mail and delete this e-mail from your system. Unless explicitly and conspicuously designated as "E-Contract Intended", this e-mail does not constitute a contract offer, a contract amendment, or an acceptance of a contract offer. This e-mail does not constitute a consent to the use of sender's contact information for direct marketing purposes or for transfers of data to third parties. Francais Deutsch Italiano Espanol Portugues Japanese Chinese Korean http://www.DuPont.com/corp/email_disclaimer.html