Class::MakeMethods::Template::Ref - Universal copy and compare methods


Class-MakeMethods documentation  | view source Contained in the Class-MakeMethods distribution.

Index


NAME

Top

Class::MakeMethods::Template::Ref - Universal copy and compare methods

SYNOPSIS

Top

  package MyObject;
  use Class::MakeMethods::Template::Ref (
    'Hash:new'      => [ 'new' ],
    clone           => [ 'clone' ]
  );

  package main;

  my $obj = MyObject->new( foo => ["Foozle", "Bozzle"] );
  my $clone = $obj->clone();
  print $obj->{'foo'}[1];

DESCRIPTION

Top

The following types of methods are provided via the Class::MakeMethods interface:

clone

Produce a deep copy of an instance of almost any underlying datatype.

Parameters:

init_method

If defined, this method is called on the new object with any arguments passed in.

prototype

Create new instances by making a deep copy of a static prototypical instance.

Parameters:

init_method

If defined, this method is called on the new object with any arguments passed in. =cut

sub prototype { ( { 'interface' => { default => { '*'=>'set_or_new', }, }, 'behavior' => { 'set_or_new' => sub { my $m_info = $_[0]; sub { my $class = shift;

	if ( scalar @_ == 1 and UNIVERSAL::isa( $_[0], $class ) ) {
	  # set
	  $m_info->{'instance'} = shift 

	} else {
	  # get
	  croak "Prototype is not defined" unless $m_info->{'instance'};
	  my $self = ref_clone($m_info->{'instance'});

	  my $init_method = $m_info->{'init_method'};
	  if ( $init_method ) {
	    $self->$init_method( @_ );
	  } elsif ( scalar @_ ) {
	    croak "No init_method";
	  }
	  return $self;
	}
      }},
      'set' => sub { my $m_info = $_[0]; sub {
	my $class = shift;
	$m_info->{'instance'} = shift 
      }},
      'new' => sub { my $m_info = $_[0]; sub {
	my $class = shift;

	croak "Prototype is not defined" unless $m_info->{'instance'};
	my $self = ref_clone($m_info->{'instance'});

	my $init_method = $m_info->{'init_method'};
	if ( $init_method ) {
	  $self->$init_method( @_ );
	} elsif ( scalar @_ ) {
	  croak "No init_method";
	}
	return $self;
      }},
    },
  } )
}

######################################################################

compare

Compare one object to another.

Templates

SEE ALSO

Top

See Class::MakeMethods for general information about this distribution.

See Class::MakeMethods::Template for more about this family of subclasses.

See Class::MakeMethods::Utility::Ref for the clone and compare functions used above.


Class-MakeMethods documentation  | view source Contained in the Class-MakeMethods distribution.