[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[FWP] Add to elements of a list



Was writing code where I had three variables which I wanted to increment by 
the three values returned from a sub.  Well of course, I can't write

	($x, $y, $z) += foo();

and neither would I want to deal with the ramifications if I could.

foo() could not be called more than once (side effects), or I might write

	$x += (foo())[0];  etc

The most 'obvious' way was something like

	{
	  my @a = foo();
	  for ($x, $y, $z) { $_ += shift @a }
	}

but I just have this thing against creating new variables, however temporary.

After much head-scratching, I came up with this monstrosity fitting the 
criteria: mustn't call foo() more than once; mustn't create any temporary 
variables (well, not by name, anyway; I don't count localized $_ and $1):

	for (foo()) { eval "$1 += $_" if '$x $y $z' =~ m/(\S+)/g }

Gross, huh?  No, I'm not planning on using it in my code.

--
Peter Scott
Pacific Systems Design Technologies


==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe