Heray-Was-Here
Server : Apache
System : Linux vps103298.mylogin.co 4.18.0-513.11.1.el8_9.x86_64 #1 SMP Wed Jan 17 02:00:40 EST 2024 x86_64
User : calvet ( 273824)
PHP Version : 7.4.33
Disable Function : NONE
Directory :  /proc/self/root/proc/thread-self/root/usr/share/doc/perl-Moose/t/cmop/lib/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //proc/self/root/proc/thread-self/root/usr/share/doc/perl-Moose/t/cmop/lib/InstanceCountingClass.pm
package # hide the package from PAUSE
    InstanceCountingClass;

use strict;
use warnings;

our $VERSION = '0.03';

use parent 'Class::MOP::Class';

InstanceCountingClass->meta->add_attribute('count' => (
    reader  => 'get_count',
    default => 0
));

InstanceCountingClass->meta->add_before_method_modifier('_construct_instance' => sub {
    my ($class) = @_;
    $class->{'count'}++;
});

1;

__END__

=pod

=head1 NAME

InstanceCountingClass - An example metaclass which counts instances

=head1 SYNOPSIS

  package Foo;

  use metaclass 'InstanceCountingClass';

  sub new  {
      my $class = shift;
      $class->meta->new_object(@_);
  }

  # ... meanwhile, somewhere in the code

  my $foo = Foo->new();
  print Foo->meta->get_count(); # prints 1

  my $foo2 = Foo->new();
  print Foo->meta->get_count(); # prints 2

  # ... etc etc etc

=head1 DESCRIPTION

This is a classic example of a metaclass which keeps a count of each
instance which is created.

=head1 AUTHORS

Stevan Little E<lt>stevan@iinteractive.comE<gt>

Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>

=cut

Hry