1Properties(3) User Contributed Perl Documentation Properties(3)
2
3
4
6 Scalar::Properties - run-time properties on scalar variables
7
9 use Scalar::Properties;
10 my $val = 0->true;
11 if ($val && $val == 0) {
12 print "yup, its true alright...\n";
13 }
14
15 my @text = (
16 'hello world'->greeting(1),
17 'forget it',
18 'hi there'->greeting(1),
19 );
20 print grep { $_->is_greeting } @text;
21
22 my $l = 'hello world'->length;
23
25 Scalar::Properties attempts to make Perl more object-oriented by taking
26 an idea from Ruby: Everything you manipulate is an object, and the
27 results of those manipulations are objects themselves.
28
29 'hello world'->length
30 (-1234)->abs
31 "oh my god, it's full of properties"->index('g')
32
33 The first example asks a string to calculate its length. The second
34 example asks a number to calculate its absolute value. And the third
35 example asks a string to find the index of the letter 'g'.
36
37 Using this module you can have run-time properties on initialized
38 scalar variables and literal values. The word 'properties' is used in
39 the Perl 6 sense: out-of-band data, little sticky notes that are
40 attached to the value. While attributes (as in Perl 5's attribute
41 pragma, and see the "Attribute::*" family of modules) are handled at
42 compile-time, properties are handled at run-time.
43
44 Internally properties are implemented by making their values into
45 objects with overloaded operators. The actual properties are then sim‐
46 ply hash entries.
47
48 Most properties are simply notes you attach to the value, but some may
49 have deeper meaning. For example, the "true" and "false" properties
50 plays a role in boolean context, as the first example of the Synopsis
51 shows.
52
53 Properties can also be propagated between values. For details, see the
54 EXPORTS section below. Here is an example why this might be desirable:
55
56 pass_on('approximate');
57 my $pi = 3->approximate(1);
58 my $circ = 2 * $rad * $pi;
59
60 # now $circ->approximate indicates that this value was derived
61 # from approximate values
62
63 Please don't use properties whose name start with an underscore; these
64 are reserved for internal use.
65
66 You can set and query properties like this:
67
68 "$var->myprop(1)"
69 sets the property to a true value.
70
71 "$var->myprop(0)"
72 sets the property to a false value. Note that this doesn't delete
73 the property (to do so, use the "del_props" method described
74 below).
75
76 "$var->is_myprop", "$var->has_myprop"
77 returns a true value if the property is set (i.e., defined and has
78 a true value). The two alternate interfaces are provided to make
79 querying attributes sound more natural. For example:
80
81 $foo->is_approximate;
82 $bar->has_history;
83
85 Values thus made into objects also expose various utility methods. All
86 of those methods (unless noted otherwise) return the result as an over‐
87 loaded value ready to take properties and method calls itself, and
88 don't modify the original value.
89
90 INTROSPECTIVE METHODS
91
92 These methods help in managing a value's properties.
93
94 "$var-"get_props>
95 Get a list of names of the value's properties.
96
97 "$var-"del_props(LIST)>
98 Deletes one or more properties from the value. This is different
99 than setting the property value to zero.
100
101 "$var-"del_all_props>
102 Deletes all of the value's properties.
103
104 NUMERICAL METHODS
105
106 "plus(EXPR)"
107 Returns the value that is the sum of the value whose method has
108 been called and the argument value. This method also overloads
109 addition, so:
110
111 $a = 7 + 2;
112 $a = 7->plus(2); # the same
113
114 "minus(EXPR)"
115 Returns the value that is the the value whose method has been
116 called minus the argument value. This method also overloads sub‐
117 traction.
118
119 "times(EXPR)"
120 Returns the value that is the the value whose method has been
121 called times the argument value. This method also overloads multi‐
122 plication.
123
124 "divide(EXPR)"
125 Returns the value that is the the value whose method has been
126 called divided by the argument value. This method also overloads
127 division.
128
129 "modulo(EXPR)"
130 Returns the value that is the the value whose method has been
131 called modulo the argument value. This method also overloads the
132 modulo operator.
133
134 "exp(EXPR)"
135 Returns the value that is the the value whose method has been
136 called powered by the argument value. This method also overloads
137 the exponentiation operator.
138
139 "abs"
140 Returns the absolute of the value.
141
142 "zero"
143 Returns a boolean value indicating whether the value is equal to 0.
144
145 STRING METHODS
146
147 "length", "size"
148 Returns the result of the built-in "length" function applied to the
149 value.
150
151 "reverse"
152 Returns the reverse string of the value.
153
154 "uc", "ucfirst", "lc", "lcfirst", "hex", "oct"
155 Return the result of the appropriate built-in function applied to
156 the value.
157
158 "concat(EXPR)", "append(EXPR)"
159 Returns the result of the argument expression appended to the
160 value.
161
162 "swapcase"
163 Returns a version of the value with every character's case
164 reversed, i.e. a lowercase character becomes uppercase and vice
165 versa.
166
167 "split /PATTERN/, LIMIT"
168 Returns a list of overloaded values that is the result of splitting
169 (according to the built-in "split" function) the value along the
170 pattern, into a number of values up to the limit.
171
172 BOOLEAN METHODS
173
174 "numcmp(EXPR)"
175 Returns the (overloaded) value of the numerical three-way compari‐
176 son. This method also overloads the "<=>" operator.
177
178 "cmp(EXPR)"
179 Returns the (overloaded) value of the alphabetical three-way com‐
180 parison. This method also overloads the "cmp" operator.
181
182 "eq(EXPR)", "ne(EXPR)", "lt(EXPR)", "gt(EXPR)", "le(EXPR)", "ge(EXPR)"
183 Return the (overlaoded) boolean value of the appropriate string
184 comparison. These methods also overload those operators.
185
186 "eqi(EXPR)", "nei(EXPR)", "lti(EXPR)", "gti(EXPR)", "lei(EXPR)",
187 "gei(EXPR)"
188 These methods are case-insensitive versions of the above operators.
189
190 "is_true", "is_false"
191 Returns the (overloaded) boolean status of the value.
192
194 Three subroutines dealing with how properties are propagated are auto‐
195 matically exported. For an example of propagation, see the DESCRIPTION
196 section above.
197
198 "pass_on(LIST)"
199 Sets (replaces) the list of properties that are passed on. There is
200 only one such list for the whole mechanism. The whole property
201 interface is experimental, but this one in particular is likely to
202 change in the future.
203
204 "passed_on(STRING)"
205 Tests whether a property is passed on and returns a boolean value.
206
207 "get_pass_on"
208 Returns a list of names of properties that are passed on.
209
211 None known so far. If you find any bugs or oddities, please do inform
212 the authors.
213
215 James A. Duncan <jduncan@fotango.com>
216
217 Marcel Grunauer, <marcel@codewerk.com>
218
219 Some contributions from David Cantrell, <david@cantrell.org.uk>
220
222 Copyright 2001 Marcel Grunauer, James A. Duncan. Portions copyright
223 2003 David Cantrell. All rights reserved.
224
225 This library is free software; you can redistribute it and/or modify it
226 under the same terms as Perl itself.
227
229 perl(1), overload(3pm), Perl 6's properties.
230
231
232
233perl v5.8.8 2003-11-09 Properties(3)