awohld has asked for the wisdom of the Perl Monks concerning the following question:

I read in a book somewhere and can't find it now, that you can use 'tie' or 'bind' to effect a scalar where every time you access it, it can alternate it's values for you.

How is this done?

I want '$color' to alternate between '#cccccc' and '#ffffff' every 10 times I call it.

So the first 10 times I use the '$color' scalar, it will return '#ffffff', and the next 10 times I access it will return '#cccccc'. And it will alternate forever.

How is this done?

  • Comment on Using 'bind' or 'tie' to automatically swap variable values.

Replies are listed 'Best First'.
Re: Using 'bind' or 'tie' to automatically swap variable values.
by Corion (Patriarch) on Jun 12, 2006 at 08:04 UTC

    I think Tie::Cycle does what you want:

    use Tie::Cycle; my @list = (("#cccccc") x 10, ("#xffffff")x10); tie my $cycle, 'Tie::Cycle', \@list;

    Be aware though that your cycle can easily go out of step if you're using the variable elsewhere, but you're likely aware of that already.

Re: Using 'bind' or 'tie' to automatically swap variable values. - DON'T DO IT!
by qbxk (Friar) on Jun 14, 2006 at 17:04 UTC
    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