Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Autoincrement;
- use warnings;
- use strict;
- # this is a constructor;
- # it sets up whatever it is we're trying to do with the variable and returns an object
- sub TIESCALAR {
- my $class = shift;
- my $realdata = 0;
- bless \$realdata, $class;
- }
- # this method defines what happens when we access the varible
- # in this case it looks inside the object reference, get the real value of the counter and increase it by one
- sub FETCH {
- my $self = shift;
- return $$self++;
- }
- # this method stores data to the variable
- # in this case, every time we attempt to store something to it, the counter goes back to zero
- sub STORE {
- # get the object
- my $self = shift;
- # get the value that was given to the tied variable; it was passed as an argument
- my $value = shift;
- warn "Hi, you said $value\n";
- $$self = 0;
- }
- 1;
Add Comment
Please, Sign In to add comment