Acme-Playpen

 view release on metacpan or  search on metacpan

lib/Tupelo/Munge.pm  view on Meta::CPAN


    use Tuple::Munge
	qw(tuple_mutable tuple_length tuple_slot tuple_slots);

    if(tuple_mutable($tuple)) { ...
    $len = tuple_length($tuple);
    $ref = tuple_slot($tuple, 3);
    @refs = tuple_slots($tuple);

    use Tuple::Munge qw(tuple_set_slot tuple_set_slots tuple_seal);

    tuple_set_slot($tuple, 3, \$s);
    tuple_set_slots($tuple, \$s, \@a, \%h, \&c);
    tuple_seal($tuple);

=head1 DESCRIPTION

This module provides functions to manipulate Perl's tuples, the data
structures that were introduced in Perl 5.37.9 to support the core class
system.  As of Perl 5.37.10, the Perl core notably lacks both general
manipulation facilities and documentation for these data structures.

Tuple data structures are experimental, so the feature could change
significantly or disappear entirely in future versions of Perl.
Be mindful of the portability issues (requiring a recent Perl and having
doubtful forward portability) in any decision about whether to use tuples.

=head2 Tuple data type

The tuple data type is a structured data type that can be used to
contain arbitrary Perl objects.  It sits alongside the array and hash
data types in Perl's type system.  A tuple is not a scalar value, and
so cannot be stored directly in a scalar variable, and in fact there is
no type of Perl variable that can directly contain a tuple.  A tuple
can be referenced through a Perl reference, which is a scalar value,
and indeed this is the only way to handle a tuple in Perl code.

At any time, a tuple has a sequence of zero or more slots, each of
which either is empty or references a Perl object.  The objects that a
tuple references may be of any type: scalar, array, hash, subroutine,
format, I/O, or tuple.  This is unlike an array or a hash, which can only
reference scalars.  The objects referenced by a tuple may simultaneously
be referenced in other places, for example by being a package variable.
A tuple slot is not itself a referencable object.  Operations on
an arbitrary slot of a tuple, identified by index, are efficient.
Operating on all the slots simultaneously is also efficient.

In general it is possible to mutate an existing tuple.  It is possible
to write to any slot within a tuple (either emptying it or writing in an
object reference), and it is also possible to replace a tuple's entire
sequence of slot values with a different one.  A tuple can be marked as
read-only to prevent any mutation.  A tuple thus being immutable only
prevents mutation of the tuple's sequence of slot values (and blessing
of the tuple); it doesn't affect mutation of the objects referenced by
the tuple.

It is possible for mutation to change the number of slots in a tuple,
but this is a relatively expensive operation that should be avoided.
Other than writing to a single slot and replacement of the complete
sequence of slot values, tuples do not naturally support operations to
edit the slot value sequence, such as adding an object reference onto
the end of a tuple.  Such an operation can be constructed via complete
replacement of a tuple's slot value sequence, but if the desire to do
that arises then it suggests that an array would be a more appropriate
data structure.

A tuple can be identified as such through the
L<C<builtin::reftype>|builtin/reftype> function, which will return
C<OBJECT> when given a reference to one.  Correspondingly, the
L<C<ref>|perlfunc/ref> function will return C<OBJECT> if given a reference
to an unblessed tuple, and the default stringification of a reference to a
tuple will include C<OBJECT>.  Beware that this usage of the word "object"
is confusing: it was already a somewhat overloaded term before the tuple
data type existed, so it makes a poor way to identify the tuple data type.

Via this module, a tuple can be constructed from a list of slot values,
and the slot values in a tuple can be read and written.  This module
represents each slot value in the form of a reference value (for a slot
referencing an object) or an undefined value (for an empty slot).

=head2 Core class system

The L<Perl core class system|perlclass> (introduced alongside tuples in
Perl 5.37.9) uses tuples as the representation format for its classful
objects.  This class system is not the subject of this module, but it
has a special relationship to the tuple data type, which is worthy of
comment.  Beware that the core documentation overloads the term "class",
using it both to refer to Perl classes in general (i.e., packages into
which objects get blessed) and to refer specifically to the classes of
this class system.

A blessed object constructed through the automatically-generated
constructor for a core class system class is always a tuple object,
and its slots reference the field variables for that class and its
superclasses.  The field variables can only be scalars, arrays, and
hashes, so those are the only types of objects that will be referenced
by such tuples.  Once the object is fully constructed and visible as
C<$self> to method code, none of the tuple slots are empty, and the
sequence of object references never subsequently changes.

Beware that mutating a tuple used as the representation of one of these
blessed objects can easily cause malfunction of its class.  Of course,
the same goes for mutating the innards of any data structure representing
a classful object.

=head2 Experimental status

The tuple data type as supplied by the Perl core is experimental.
This means that it could change significantly, or be removed entirely,
in a future version of Perl.

Furthermore, the manner in which this module uses the tuple data type
is somewhat speculative.  The Perl core does not document precisely what
kinds of operations are intended to be possible on tuples, and there is no
established common practice, so the semantics offered by this module are
in part guesses.  For example, rewriting a tuple in a way that changes
the number of slots it has is something that's naturally possible to do
with the data structure, but isn't ever performed by core code, and it
might be decided in the future that it should never happen to a tuple
beyond its initial construction.



( run in 3.238 seconds using v1.01-cache-2.11-cpan-13bb782fe5a )