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

In reply to Re: Using 'bind' or 'tie' to automatically swap variable values. - DON'T DO IT! by qbxk
in thread Using 'bind' or 'tie' to automatically swap variable values. by awohld

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.