1MakeMethods::Template::URseefr(3C)ontributed Perl DocumeMnatkaetMieotnhods::Template::Ref(3)
2
3
4

NAME

6       Class::MakeMethods::Template::Ref - Universal copy and compare methods
7

SYNOPSIS

9         package MyObject;
10         use Class::MakeMethods::Template::Ref (
11           'Hash:new'      => [ 'new' ],
12           clone           => [ 'clone' ]
13         );
14
15         package main;
16
17         my $obj = MyObject->new( foo => ["Foozle", "Bozzle"] );
18         my $clone = $obj->clone();
19         print $obj->{'foo'}[1];
20

DESCRIPTION

22       The following types of methods are provided via the Class::MakeMethods
23       interface:
24
25       clone
26
27       Produce a deep copy of an instance of almost any underlying datatype.
28
29       Parameters:
30
31       init_method
32
33       If defined, this method is called on the new object with any arguments
34       passed in.
35
36       prototype
37
38       Create new instances by making a deep copy of a static prototypical
39       instance.
40
41       Parameters:
42
43       init_method
44
45       If defined, this method is called on the new object with any arguments
46       passed in.  =cut
47
48       sub prototype {
49         ( {
50           'interface' => {
51             default => { '*'=>'set_or_new',  },
52           },
53           'behavior' => {
54             'set_or_new' => sub { my $m_info = $_[0]; sub {      my $class =
55       shift;
56
57               if ( scalar @_ == 1 and UNIVERSAL::isa( $_[0], $class ) ) {
58                 # set
59                 $m_info->{'instance'} = shift
60
61               } else {
62                 # get
63                 croak "Prototype is not defined" unless $m_info->{'instance'};
64                 my $self = ref_clone($m_info->{'instance'});
65
66                 my $init_method = $m_info->{'init_method'};
67                 if ( $init_method ) {
68                   $self->$init_method( @_ );
69                 } elsif ( scalar @_ ) {
70                   croak "No init_method";
71                 }
72                 return $self;
73               }
74             }},
75             'set' => sub { my $m_info = $_[0]; sub {
76               my $class = shift;
77               $m_info->{'instance'} = shift
78             }},
79             'new' => sub { my $m_info = $_[0]; sub {
80               my $class = shift;
81
82               croak "Prototype is not defined" unless $m_info->{'instance'};
83               my $self = ref_clone($m_info->{'instance'});
84
85               my $init_method = $m_info->{'init_method'};
86               if ( $init_method ) {
87                 $self->$init_method( @_ );
88               } elsif ( scalar @_ ) {
89                 croak "No init_method";
90               }
91               return $self;
92             }},
93           },
94         } )
95       }
96
97       ######################################################################
98
99       compare
100
101       Compare one object to another.
102
103       Templates
104
105       ·   default
106
107           Three-way (sorting-style) comparison.
108
109       ·   equals
110
111           Are these two objects equivalent?
112
113       ·   identity
114
115           Are these two references to the exact same object?
116

SEE ALSO

118       See Class::MakeMethods for general information about this distribution.
119
120       See Class::MakeMethods::Template for more about this family of sub‐
121       classes.
122
123       See Class::MakeMethods::Utility::Ref for the clone and compare func‐
124       tions used above.
125
126
127
128perl v5.8.8                       2004-09-06     MakeMethods::Template::Ref(3)
Impressum