in reply to Using 'bind' or 'tie' to automatically swap variable values.

That's a clever idea, and a neat trick, but my advice is don't do it, it's not maintainable*, and if you're writing code that's not easily maintainable it's for one of two reasons (the other points on "the triangle")
1) you need it quick (now!!) or
2) you need it to run fast (not really an applicable reason for most cases involving the use of perl anyhow..imho)

now, having said that and you agree you want to make it maintainable, here's what I'd do. It looks like you're wanting the have alternating colors for your rows in some html table, so my common approach is..
#untested... my @colors = ('#cccccc', '#ffffff', 'lightgreen'); my $index = 0; foreach( @rows ) { my $bgcolor = $colors[ $index % @colors ]; print qq{<tr style="background-color: $bgcolor;">}; # etc... print qq{</tr>}; $index++; }
it's all about the modulo, yo! "%"

* Why is your "neat trick" not maintainable? because it's arcane and not exactly self-documenting, IOW it's "surprising" therefore violating the principle of least surprise. Somebody else may come along to fix or change this and not know what the hell is going on, and, in fact, that somebody else may just be the future you (who could be a decidely different being than the present you and be cursing the present you all the same). For example a common thing i do in these kinds of tables is to add a 2nd "<tr>" tag that's still "the same row" to the humans, but maybe has a <td> with a colspan attribute or something, now you add your tied $color variable to the 2nd <tr> and boom, "hey it's a different color, what to do?" gotta refactor your engine now, buddy, d'oh!

It's not what you look like, when you're doin' what you’re doin'.
It's what you’re doin' when you’re doin' what you look like you’re doin'!
     - Charles Wright & the Watts 103rd Street Rhythm Band, Express yourself
  • Comment on Re: Using 'bind' or 'tie' to automatically swap variable values. - DON'T DO IT!
  • Download Code