NAME

Marlin::X::Clone - Marlin extension to add a clone method to your class.

SYNOPSIS

package Local::Date {
  use Marlin qw( year month day :Clone );
}

my $xmas     = Local::Date->new( day => 25, month => 12, year => 2025 );
my $xmas_eve = $xmas->clone( day => 24 );

DESCRIPTION

This package creates a method in your class that does roughly:

sub clone {
  my ( $self, %args ) = @_;
  my %clone = ( %$self, %args );
  return bless \%clone, ref($self);
}

Except it also:

  • Skips over "PRIVATE" storage attributes by default.

  • Respects the init_arg for each attribute.

  • Calls trigger methods, uses defaults, checks type constraints, and does type coercions.

  • Allows per-attribute tweaks to its behaviour.

  • Calls BUILD methods.

  • Complains if you give it arguments which it doesn't recognize.

You can tweak an attribute's behaviour using:

use Marlin foo => { on_clone => ... };

The on_clone option can be set to "1" if you wish to allow that attribute to be copied to clones (this is the default except for "PRIVATE" stored attributes), set to "0" to forbid if from being copied to clones, set to ":deep" to make a deep clone of the attibute's value, or set to a coderef or the name of a method to make a custom clone of the attribute's value. (The coderef or method will be passed the name of the attribute and the attribute's value as parameters and should return the cloned value.)

You can also set a few options for how the plugin behaves:

use Marlin qw( foo bar ),
  ':Clone' => {
    method_name     => 'clone', # Name for the clone method
    call_build      => true,    # Call BUILD after cloning?
    strict_args     => true,    # Complain about unrecognized params?
  };

This module also acts as a demonstration of Marlin's extension API. See comments in the source code for more details.

BUGS

Please report any bugs to https://github.com/tobyink/p5-marlin/issues.

SEE ALSO

Marlin.

AUTHOR

Toby Inkster <tobyink@cpan.org>.

COPYRIGHT AND LICENCE

This software is copyright (c) 2025 by Toby Inkster.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

DISCLAIMER OF WARRANTIES

THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.

🐟🐟