Tie::FlipFlop - Alternate between two values.


Tie-FlipFlop documentation Contained in the Tie-FlipFlop distribution.

Index


Code Index:

NAME

Top

Tie::FlipFlop - Alternate between two values.

SYNOPSIS

Top

    use Tie::FlipFlop;

    tie my $flipflop => Tie::FlipFlop => qw /Red Green/;

    print  $flipflop;      # Prints 'Red'.
    print  $flipflop;      # Prints 'Green'.
    print  $flipflop;      # Prints 'Red'.

DESCRIPTION

Top

Tie::FlipFlop allows you to tie a scalar in such a way that refering to the scalar alternates between two values. When tying the scalar, exactly two extra arguments have to be given, the values to be alternated between.

Assigning to the scalar leads to a fatal, but trappable, error.

DEVELOPMENT

Top

The current sources of this module are found on github, git://github.com/Abigail/tie--flipflop.git.

AUTHOR

Top

Abigail <cpan@abigail.be>.

COPYRIGHT and LICENSE

Top


Tie-FlipFlop documentation Contained in the Tie-FlipFlop distribution.

package Tie::FlipFlop;

use 5.006;

use strict;
use warnings;
no  warnings 'syntax';


our $VERSION = '2009110701';


sub TIESCALAR {
    my $class = shift;
    do {require Carp;
        Carp::croak ("Incorrect number of arguments");
    } unless 2 == @_;
    bless [reverse @_] => $class;
}

sub FETCH     {
    my $state = shift;
     (@$state = reverse @$state) [0]
}

sub STORE     {
    require Carp;
    Carp::croak ("Cannot modify read only variable");
}


1;


__END__