Ancient

 view release on metacpan or  search on metacpan

lib/slot.pm  view on Meta::CPAN


=head2 slot::set_by_idx

    slot::set_by_idx($idx, $value);

Set a slot value by numeric index. Faster than variable name lookup.
Watchers are still triggered.

=head2 slot::watch

    slot::watch('name', sub { my ($name, $val) = @_; ... });

Register a callback that fires whenever the slot value changes.

B<Compile-time optimization:> When called with a constant string,
optimized to a custom op.

=head2 slot::unwatch

    slot::unwatch('name');            # Remove all watchers
    slot::unwatch('name', $coderef);  # Remove specific watcher

B<Compile-time optimization:> When called with a constant string,
optimized to a custom op.

=head2 slot::clear

    slot::clear('name');
    slot::clear('name1', 'name2');

Reset slot value(s) to undef and remove all associated watchers.
The slot still exists (can be set again), but its value is cleared.

B<Compile-time optimization:> When called with a single constant string,
optimized to a custom op.

=head2 slot::clear_by_idx

    slot::clear_by_idx($idx);
    slot::clear_by_idx($idx1, $idx2);

Reset slot value(s) to undef and remove watchers by numeric index.

=head2 slot::slots

    my @names = slot::slots();

Returns a list of all defined slot names.

=head2 slot::exists

    if (slot::exists('config')) {
        # slot is defined
    }

Check if a slot with the given name has been defined. Returns true if
the slot exists, false otherwise.

=head1 THREAD SAFETY

For thread-safe data sharing, store C<threads::shared> variables in slots:

    use threads;
    use threads::shared;
    use slot qw(config);

    # Create shared data and store in slot
    my %shared :shared;
    $shared{counter} = 0;
    config(\%shared);

    # Threads can now safely share data via the slot
    my @threads = map {
        threads->create(sub {
            my $cfg = config();
            lock(%$cfg);
            $cfg->{counter}++;
        });
    } 1..10;

    $_->join for @threads;
    print config()->{counter};  # 10

The slot provides the global accessor; C<threads::shared> provides the
thread-safe storage.

=head1 FORK BEHAVIOR

After C<fork()>, child processes get a copy of slot values (copy-on-write).
Changes in child processes do not affect the parent, and vice versa.

=head1 AUTHOR

LNATION <email@lnation.org>

=head1 LICENSE

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut



( run in 0.609 second using v1.01-cache-2.11-cpan-f889d44b568 )