daily pastebin goal
28%
SHARE
TWEET

Untitled

a guest Feb 20th, 2018 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package Autoincrement;
  2. use warnings;
  3. use strict;
  4.  
  5. # this is a constructor;
  6. # it sets up whatever it is we're trying to do with the variable and returns an object
  7. sub TIESCALAR {
  8.     my $class = shift;
  9.     my $realdata = 0;
  10.     bless \$realdata, $class;
  11. }
  12.  
  13. # this method defines what happens when we access the varible
  14. # in this case it looks inside the object reference, get the real value of the counter and increase it by one
  15. sub FETCH {
  16.     my $self = shift;
  17.     return $$self++;
  18. }
  19.  
  20. # this method stores data to the variable
  21. # in this case, every time we attempt to store something to it, the counter goes back to zero
  22. sub STORE {
  23.     # get the object
  24.     my $self = shift;
  25.     # get the value that was given to the tied variable; it was passed as an argument
  26.     my $value = shift;
  27.     warn "Hi, you said $value\n";
  28.     $$self = 0;
  29. }
  30.  
  31. 1;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top