Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

FETCHing twice in Tie::FlipFlop

by CharlesClarkson (Curate)
on Dec 19, 2003 at 23:18 UTC ( #315946=perlquestion: print w/replies, xml ) Need Help??

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?

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.

    "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.

    $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.


    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.


      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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://315946]
Approved by Corion
Front-paged by traveler
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2020-09-25 21:52 GMT
Find Nodes?
    Voting Booth?
    If at first I donít succeed, I Ö

    Results (141 votes). Check out past polls.