1 2=head1 NAME 3 4Set::Object::Weak - Sets without the referant reference increment 5 6=head1 SYNOPSIS 7 8 use Set::Object::Weak qw(weak_set); 9 10 my $set = Set::Object::Weak->new( 0, "", {}, [], $object ); 11 # or 12 my $set = weak_set( 0, "", {}, [], $object ); 13 14 print $set->size; # 2 - the scalars aren't objects 15 16=head1 DESCRIPTION 17 18Sets, but weak. See L<Set::Object/weaken>. 19 20Note that the C<set> in C<Set::Object::Weak> returns weak sets. This 21is intentional, so that you can make all the sets in scope weak just 22by changing C<use Set::Object> to C<use Set::Object::Weak>. 23 24=cut 25 26package Set::Object::Weak; 27use strict; 28use base qw(Set::Object); # boo hiss no moose::role yet I hear you say 29 30use base qw(Exporter); # my users would hate me otherwise 31use vars qw(@ISA @EXPORT_OK); 32use Set::Object qw(blessed); 33 34our @EXPORT_OK = qw(weak_set set); 35 36=head1 CONSTRUCTORS 37 38=over 39 40=item new 41 42This class method is exactly the same as C<Set::Object-E<gt>new>, 43except that it returns a weak set. 44 45=cut 46 47sub new { 48 my $class = shift; 49 my $self = $class->SUPER::new(); 50 $self->weaken; 51 $self->insert(@_); 52 $self; 53} 54 55=item weak_set( ... ) 56 57This optionally exported B<function> is a shortcut for saying 58C<Set::Object::Weak-E<gt>new(...)>. 59 60=cut 61 62 63sub weak_set { 64 __PACKAGE__->new(@_); 65} 66 67=item set( ... ) 68 69This method is exported so that if you see: 70 71 use Set::Object qw(set); 72 73You can turn it into using weak sets lexically with: 74 75 use Set::Object::Weak qw(set); 76 77Set::Object 1.19 had a bug in this method that meant that it would not 78add the passed members into it. 79 80=cut 81 82sub set { 83 my $class = __PACKAGE__; 84 if (blessed $_[0] and $_[0]->isa("Set::Object")) { 85 $class = (shift)->strong_pkg; 86 } 87 $class->new(@_); 88} 89 901; 91 92__END__ 93 94=back 95 96=head1 SEE ALSO 97 98L<Set::Object> 99 100=head1 CREDITS 101 102Perl magic by Sam Vilain, <samv@cpan.org> 103 104Idea from nothingmuch. 105