<commit_before>use strict;
use warnings;
package Tie::Scalar::Callback;

use parent 'Tie::Scalar';

#ABSTRACT: a tied scalar which executes a callback everytime it is used

sub TIESCALAR {
  my ($class, $sub, $val) = @_;

  die 'Must provide an anonymous subroutine argument'
    unless $sub && ref $sub eq 'CODE';

  bless {
    sub => $sub,
    val => $val,
  }, $class;
}

sub STORE {
  my ($self, $val) = @_;
  return $self->{sub}($self, 'STORE', $val);
}

sub FETCH {
  my ($self) = @_;
  return $self->{sub}($self, 'FETCH');
}

sub DESTROY {
  my ($self) = @_;
  return $self->{sub}($self, 'DESTROY');
}

1;

=head1 SYNOPSIS

  use Tie::Scalar::Callback;

  # this coderef doubles the scalar's value everytime it's fetched 
  my $coderef = sub {
    my ($self, $event, $val) = @_;

    if ($event eq 'STORE')
    {
      $self->{val} = $val;
    }
    elsif ($event eq 'FETCH')
    {
      my $old_val = $self->{val};
      $self->{val} *= 2;
      return $old_val;
    }
    else # DESTROY
    {
      undef $self;
    }
  };

  tie(my $doubler, 'Tie::Scalar::Callback', $coderef, 1);

  print $doubler; 1
  print $doubler; 2
  print $doubler; 4

=head1 DESCRIPTION

C<Tie::Scalar::Callback> is a class for creating tied scalars which execute
a callback everytime an event occurs on the scalar. There are three types of
event:

=over 4

=item * STORE ($self, 'STORE', $value)

Called anytime a value is assigned to the scalar.

=item * FETCH ($self, 'FETCH')

Called anytime the scalar's value is retrieved.

=item * DESTROY ($self, 'DESTROY')

Called on object destruction.

=back

See the synopsis for an example coderef which handles these events.

=head1 INTERNALS

C<Tie::Scalar::Callback> objects are just anonymous hashes. The coderef is
stored in C<$self->{sub}> and the current value of the scalar in
C<$self->{val}>.

=head1 SEE ALSO

L<Tie::Scalar::Decay|https://metacpan.org/pod/Tie::Scalar::Decay>
L<Tie::Scalar::Ratio|https://metacpan.org/pod/Tie::Scalar::Ratio>

<commit_msg>A > in a pod directive needs doubled delimiters<commit_after>use strict;
use warnings;
package Tie::Scalar::Callback;

use parent 'Tie::Scalar';

#ABSTRACT: a tied scalar which executes a callback everytime it is used

sub TIESCALAR {
  my ($class, $sub, $val) = @_;

  die 'Must provide an anonymous subroutine argument'
    unless $sub && ref $sub eq 'CODE';

  bless {
    sub => $sub,
    val => $val,
  }, $class;
}

sub STORE {
  my ($self, $val) = @_;
  return $self->{sub}($self, 'STORE', $val);
}

sub FETCH {
  my ($self) = @_;
  return $self->{sub}($self, 'FETCH');
}

sub DESTROY {
  my ($self) = @_;
  return $self->{sub}($self, 'DESTROY');
}

1;

=head1 SYNOPSIS

  use Tie::Scalar::Callback;

  # this coderef doubles the scalar's value everytime it's fetched
  my $coderef = sub {
    my ($self, $event, $val) = @_;

    if ($event eq 'STORE')
    {
      $self->{val} = $val;
    }
    elsif ($event eq 'FETCH')
    {
      my $old_val = $self->{val};
      $self->{val} *= 2;
      return $old_val;
    }
    else # DESTROY
    {
      undef $self;
    }
  };

  tie(my $doubler, 'Tie::Scalar::Callback', $coderef, 1);

  print $doubler; 1
  print $doubler; 2
  print $doubler; 4

=head1 DESCRIPTION

C<Tie::Scalar::Callback> is a class for creating tied scalars which execute
a callback everytime an event occurs on the scalar. There are three types of
event:

=over 4

=item * STORE ($self, 'STORE', $value)

Called anytime a value is assigned to the scalar.

=item * FETCH ($self, 'FETCH')

Called anytime the scalar's value is retrieved.

=item * DESTROY ($self, 'DESTROY')

Called on object destruction.

=back

See the synopsis for an example coderef which handles these events.

=head1 INTERNALS

C<Tie::Scalar::Callback> objects are just anonymous hashes. The coderef is
stored in C<< $self->{sub} >> and the current value of the scalar in
C<< $self->{val} >>.

=head1 SEE ALSO

L<Tie::Scalar::Decay|https://metacpan.org/pod/Tie::Scalar::Decay>
L<Tie::Scalar::Ratio|https://metacpan.org/pod/Tie::Scalar::Ratio>


{'fraction_non_alphanumeric': 0.1524163568773234, 'fraction_numerical': 0.005452292441140025, 'mean_word_length': 3.5044642857142856, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 38, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '784142', 'n_tokens_mistral': 1552, 'n_tokens_neox': 1469, 'n_words': 463}