Class-MakeMethods

 view release on metacpan or  search on metacpan

MakeMethods/Template/Universal.pm  view on Meta::CPAN

For examples of usage, see the test scripts in t/*closure.t.

=item *

--lvalue

Adds the ":lvalue" attribute to the subroutine declaration. 

For examples of usage, see the test scripts in t/*lvalue.t.

=item *

--warn_calls

For diagnostic purposes, call warn with the object reference, method name, and arguments before executing the body of the method.


=back


B<Behaviors>

=over 4

=item *

attributes

Runtime access to method parameters.

=item *

no_op -- See below.

=item *

croak -- See below.

=item *

method_init -- See below.

=back

=cut

sub generic { 
  {
    'code_expr' => { 
      '_SELF_' => '$self',
      '_SELF_CLASS_' => '(ref _SELF_ || _SELF_)',
      '_SELF_INSTANCE_' => '(ref _SELF_ ? _SELF_ : undef)',
      '_CLASS_FROM_INSTANCE_' => '(ref _SELF_ || croak "Can\'t invoke _STATIC_ATTR_{name} as a class method")',
      '_ATTR_{}' => '$m_info->{*}',
      '_STATIC_ATTR_{}' => '_ATTR_{*}',
      '_ATTR_REQUIRED_{}' => 
	'(_ATTR_{*} or Carp::croak("No * parameter defined for _ATTR_{name}"))',
      '_ATTR_DEFAULT_{}' => 
	sub { my @a = split(' ',$_[0],2); "(_ATTR_{$a[0]} || $a[1])" },
      
      _ACCESS_PRIVATE_ => '( ( (caller)[0] eq _ATTR_{target_class} ) or croak "Attempted access to private method _ATTR_{name}")',
      _ACCESS_PROTECTED_ => '( UNIVERSAL::isa((caller)[0], _ATTR_{target_class}) or croak "Attempted access to protected method _ATTR_{name}" )',

      '_CALL_METHODS_FROM_HASH_' => q{
	  # Accept key-value attr list, or reference to unblessed hash of attrs
	  my @args = (scalar @_ == 1 and ref($_[0]) eq 'HASH') ? %{$_[0]} : @_;
	  while ( scalar @args ) { local $_ = shift(@args); $self->$_( shift(@args) ) }
      },
      
    },
    'modifier' => {
      'self_closure' => q{ my @args = @_; return sub { unshift @_, @args; * } },
      'warn_calls' => q{ warn $self."->_STATIC_ATTR_{name}(".join(', ',@_).")\n"; * },
      'public' => q{ * },
      'private' => q{ _ACCESS_PRIVATE_; * },
      'protected' => q{ _ACCESS_PROTECTED_; * },
      '-folding' => [ 
	# Public is the default; all three options are mutually exclusive.
	'-public' => '',
	'-private -public' => '-public',
	'-protected -public' => '-public',
	'-private -protected' => '-protected',
	'-protected -private' => '-private',
      ],
      'lvalue' => { _SUB_ATTRIBS_ => ': lvalue' },
    },
    'behavior' => {
      -import => {
	'Template::Universal:no_op' => 'no_op',
	'Template::Universal:croak' => 'croak',
	'Template::Universal:method_init' => 'method_init',
      },
      attributes => sub { 
	my $m_info = $_[0]; 
	return sub {
	  my $self = shift;
	  if ( scalar @_ == 0 ) {
	    return $m_info;
	  } elsif ( scalar @_ == 1 ) {
	    return $m_info->{ shift() };
	  } else {
	    %$m_info = ( %$m_info, @_ );
	  }
	}
      },
    },
  }
}

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

=head2 no_op

For each meta-method, creates a method with an empty body.

  use Class::MakeMethods::Template::Universal (
    'no_op' => [ 'foo bar baz' ],
  );

You might want to create and use such methods to provide hooks for
subclass activity.



( run in 0.681 second using v1.01-cache-2.11-cpan-ceb78f64989 )