This and other RFCs are available on the web at
  http://dev.perl.org/rfc/

=head1 TITLE

Deep Copying, aka, cloning around.

=head1 VERSION

     Maintainer: Peter Scott <[EMAIL PROTECTED]>
     Date: 8 Aug 2000
     Version: 1
     Mailing List: [EMAIL PROTECTED]
     Number: 67

=head1 ABSTRACT

Perl should have a C<clone> method for deep copying of hierarchical data 
structures.

Damian Conway mooted a C<clone> function for deep copying of objects as 
part of an earlier discussion.  This RFC expands upon that and gives it 
special attention.  I expect several iterations of this RFC from feedback.

=head1 DESCRIPTION

The language interface is the C<clone> method which takes an object as 
argument and returns a copy of it as result:

=over 4

=item clone SCALAR

=item clone SCALAR, CALLBACK

     $copy = clone($obj)

     $copy = clone($obj, sub { die 'Filehandle copying not allowed' })

The C<CALLBACK> is an optional function to be called if C<clone> encounters 
a filehandle.  It will either throw an exception or return a value to be 
used as the cloned filehandle.

=back

=head1 IMPLEMENTATION

=head1 Default

The default functionality will switch on C<ref SCALAR> and recursively 
create new data structures in the way many people have written themselves:

  (/^$/ || /^CODE$/) && return $obj;
   /^SCALAR|REF$/    && return \clone $$obj;
   /^ARRAY$/         && return [ map clone($_), @$obj ];
   /^HASH$/          && return { map {$_, clone $obj->{$_}} keys %$obj };

TODO: behavior for C<GLOB>, C<REGEX>, and C<LVALUE>.

=head2 Blessed objects

If C<clone> encounters a blessed object C<$obj> say, it will call
C<$obj->CLONE(CALLBACK)>.  If the C<CLONE> method is not defined in 
C<$obj>'s class or any of its superclasses, C<UNIVERSAL::CLONE> will carry 
out the default functionality on the internal representation of C<$obj>.

Classes (e.g., DBI) may well choose to throw an exception in their C<CLONE> 
methods.

=head2 Filehandles

If C<clone> encounters an C<IO::Handle>, its default behavior will be to 
make a copy of the filehandle (debatable: perhaps the default should be to 
throw an exception) unless a C<CALLBACK> function was specified

=head2 Tied variables

If C<clone> encounters a tied variable, it will call the C<CLONE> method in 
that class or fall back to C<UNIVERSAL::CLONE>.  (Note: perhaps all missing 
functions from tied classes should punt to C<UNIVERSAL>.  But that is 
outside the scope of this RFC.)

=head2 Cyclic references

The C<clone> function should detect circular references and replicate the 
same structure in the copy.  One implementation that suggests itself is to 
keep a hash of input references with output references as values.  It has 
been suggested that the code to do this will already be available in the 
garbage collector.

=head2 Exceptions

If an exception is thrown anywhere during the copying, it needs to be 
trapped so that C<clone> can clean up any cyclic references it has created, 
then rethrown.

=head1 REFERENCES

None.

Reply via email to