Scalar::Properties - run-time properties on scalar variables |
Scalar::Properties - run-time properties on scalar variables
use Scalar::Properties; my $val = 0->true; if ($val && $val == 0) { print "yup, its true alright...\n"; }
my @text = ( 'hello world'->greeting(1), 'forget it', 'hi there'->greeting(1), ); print grep { $_->is_greeting } @text;
my $l = 'hello world'->length;
Scalar::Properties attempts to make Perl more object-oriented by taking an idea from Ruby: Everything you manipulate is an object, and the results of those manipulations are objects themselves.
'hello world'->length (-1234)->abs "oh my god, it's full of properties"->index('g')
The first example asks a string to calculate its length. The second example asks a number to calculate its absolute value. And the third example asks a string to find the index of the letter 'g'.
Using this module you can have run-time properties on initialized
scalar variables and literal values. The word 'properties' is used
in the Perl 6 sense: out-of-band data, little sticky notes that
are attached to the value. While attributes (as in Perl 5's attribute
pragma, and see the Attribute::*
family of modules) are handled
at compile-time, properties are handled at run-time.
Internally properties are implemented by making their values into objects with overloaded operators. The actual properties are then simply hash entries.
Most properties are simply notes you attach to the value, but some
may have deeper meaning. For example, the true
and false
properties plays a role in boolean context, as the first example
of the Synopsis shows.
Properties can also be propagated between values. For details, see the EXPORTS section below. Here is an example why this might be desirable:
pass_on('approximate'); my $pi = 3->approximate(1); my $circ = 2 * $rad * $pi;
# now $circ->approximate indicates that this value was derived # from approximate values
Please don't use properties whose name start with an underscore; these are reserved for internal use.
You can set and query properties like this:
$var->myprop(1)
$var->myprop(0)
del_props
method described
below).
$var->is_myprop
, $var->has_myprop
$foo->is_approximate; $bar->has_history;
Values thus made into objects also expose various utility methods. All of those methods (unless noted otherwise) return the result as an overloaded value ready to take properties and method calls itself, and don't modify the original value.
These methods help in managing a value's properties.
$var-
get_props>$var-
del_props(LIST)>$var-
del_all_props>
plus(EXPR)
$a = 7 + 2; $a = 7->plus(2); # the same
minus(EXPR)
times(EXPR)
divide(EXPR)
modulo(EXPR)
exp(EXPR)
abs
zero
length
, size
length
function applied to
the value.
reverse
uc
, ucfirst
, lc
, lcfirst
, hex
, oct
concat(EXPR)
, append(EXPR)
swapcase
split /PATTERN/, LIMIT
split
function) the value along the
pattern, into a number of values up to the limit.
numcmp(EXPR)
<=>
operator.
cmp(EXPR)
cmp
operator.
eq(EXPR)
, ne(EXPR)
, lt(EXPR)
, gt(EXPR)
, le(EXPR)
,
ge(EXPR)
eqi(EXPR)
, nei(EXPR)
, lti(EXPR)
, gti(EXPR)
,
lei(EXPR)
, gei(EXPR)
is_true
, is_false
Three subroutines dealing with how properties are propagated are automatically exported. For an example of propagation, see the DESCRIPTION section above.
pass_on(LIST)
passed_on(STRING)
get_pass_on
None known so far. If you find any bugs or oddities, please do inform the authors.
James A. Duncan <jduncan@fotango.com>
Marcel Grunauer, <marcel@codewerk.com>
Some contributions from David Cantrell, <david@cantrell.org.uk>
Copyright 2001 Marcel Grunauer, James A. Duncan. Portions copyright 2003 David Cantrell. All rights reserved.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
perl(1), overload(3pm), Perl 6's properties.
Scalar::Properties - run-time properties on scalar variables |