http://www.perlmonks.org?node_id=315946

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

While using Tie::FlipFlop, I ran into a problem.

use Tie::FlipFlop; tie my $flipflop => Tie::FlipFlop => ( 'white', 'black' ); print qq|$flipflop\n|; print qq|$flipflop\n|; print qq|$flipflop\n|; print qq|$flipflop\n|;
Which prints:
black black black black
While this:
use Tie::FlipFlop; tie my $flipflop => Tie::FlipFlop => ( 'white', 'black' ); print $flipflop, qq|\n|; print $flipflop, qq|\n|; print $flipflop, qq|\n|; print $flipflop, qq|\n|;
Prints this:
white black white black

After a little investigating, I found that, when quoted, FETCH was being called twice in the module for each print. Why is the value fetched twice? And can this be trapped to return the desired result?

TIA,
Charles K. Clarkson
--
Head Bottle Washer,
Clarkson Energy Homes, Inc.
Mobile Home Specialists
254 968-8328

Replies are listed 'Best First'.
Re: FETCHing twice in Tie::FlipFlop
by sauoq (Abbot) on Dec 20, 2003 at 00:51 UTC

    It isn't the quoting exactly. Removing the newlines in the quoted strings makes it work too.

    $ perl -MTie::FlipFlop -le 'tie my $f => Tie::FlipFlop => ("white","bl +ack"); print qq[$f]; print qq[$f]; print qq[$f]; print qq[$f]' white black white black
    A little more experimentation shows that it breaks anytime there is a backwhack-escaped character in the string.

    It isn't the platform, it's the version. I did my testing with 5.6.1 on Solaris and reproduced your results. This bug appears to be fixed in newer versions (5.8.0) of perl.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: FETCHing twice in Tie::FlipFlop
by Sandy (Curate) on Dec 20, 2003 at 17:04 UTC
    I also did some testing with ActiveState Perl 5.6.1

    If you use the flipflopped var in a string, and if there is a space (any whitespace) immediately following the flipflop variable, then it will be flipped twice.

    I also tested if concatenating a space causes the same problem (not shown in test code below) and it did not.

    Examples:
    $f="$flipflop "; doesn't work.
    $f="$flipflop\n"; doesn't work.
    $f="$flipflop"; works
    $f="{$flipflop}_xxx "; works
    $f="\n$flipflop"; works

    To test this, I modified the tie::flipflop module to print Doing a fetch, with state... everytime the state is flipped.

    Note: I used readmore tags around the code and results, but I can't see if they are really working until I create my post. so if they are not, I apologize.

    My test script is below

    and the results are below UPDATE: Just added a little example at beginning of post.

    QUESTION:

    Is the FETCH method called twice for all tied objects, in the conditions noted above?

    My curiosity is truly peaked. Since I have no real experience 'tieing' anything, it would take some time for me to test. Maybe someone else would be willing?...

Re: FETCHing twice in Tie::FlipFlop
by eclark (Scribe) on Dec 20, 2003 at 00:03 UTC
    I'm sorry I cannot help you. I tried to duplicate the problem, but it works correctly for me in the first case. My perl is 5.8.0 built for i386-linux-thread-multi.
      Darn! I should have tested it on a linux machine. I'm using ActiveState perl 5.6.1 build 623 on a Win XP machine.
Re: FETCHing twice in Tie::FlipFlop
by petdance (Parson) on Dec 21, 2003 at 17:40 UTC
    I don't know this Tie::FlipFlop, but it sounds like you could easily replace it with Tie::Cycle.

    xoxo,
    Andy

      The problem is a bug in concat in perl, and would equally apply to Tie::Cycle. It was fixed as patch #10223 in 5.8.0.