File: Weak.pm

package info (click to toggle)
libset-object-perl 1.43-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 620 kB
  • sloc: perl: 1,069; makefile: 14
file content (104 lines) | stat: -rw-r--r-- 1,873 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104

=head1 NAME

Set::Object::Weak - Sets without the referant reference increment

=head1 SYNOPSIS

 use Set::Object::Weak qw(weak_set);

 my $set = Set::Object::Weak->new( 0, "", {}, [], $object );
 # or
 my $set = weak_set( 0, "", {}, [], $object );

 print $set->size;  # 2 - the scalars aren't objects

=head1 DESCRIPTION

Sets, but weak.  See L<Set::Object/weaken>.

Note that the C<set> in C<Set::Object::Weak> returns weak sets.  This
is intentional, so that you can make all the sets in scope weak just
by changing C<use Set::Object> to C<use Set::Object::Weak>.

=cut

package Set::Object::Weak;
use strict;
use base qw(Set::Object);  # boo hiss no moose::role yet I hear you say

use base qw(Exporter);     # my users would hate me otherwise
use vars qw(@ISA @EXPORT_OK);
use Set::Object qw(blessed);

our @EXPORT_OK = qw(weak_set set);

=head1 CONSTRUCTORS

=over

=item new

This class method is exactly the same as C<Set::Object-E<gt>new>,
except that it returns a weak set.

=cut

sub new {
    my $class = shift;
    my $self = $class->SUPER::new();
    $self->weaken;
    $self->insert(@_);
    $self;
}

=item weak_set( ... )

This optionally exported B<function> is a shortcut for saying
C<Set::Object::Weak-E<gt>new(...)>.

=cut


sub weak_set {
    __PACKAGE__->new(@_);
}

=item set( ... )

This method is exported so that if you see:

 use Set::Object qw(set);

You can turn it into using weak sets lexically with:

 use Set::Object::Weak qw(set);

Set::Object 1.19 had a bug in this method that meant that it would not
add the passed members into it.

=cut

sub set {
    my $class = __PACKAGE__;
    if (blessed $_[0] and $_[0]->isa("Set::Object")) {
    	$class = (shift)->strong_pkg;
    }
    $class->new(@_);
}

1;

__END__

=back

=head1 SEE ALSO

L<Set::Object>

=head1 CREDITS

Perl magic by Sam Vilain, <samv@cpan.org>

Idea from nothingmuch.