<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">
package Class::MOP::Method;
BEGIN {
  $Class::MOP::Method::AUTHORITY = 'cpan:STEVAN';
}
{
  $Class::MOP::Method::VERSION = '2.0603';
}

use strict;
use warnings;

use Carp         'confess';
use Scalar::Util 'weaken', 'reftype', 'blessed';

use base 'Class::MOP::Object';

# NOTE:
# if poked in the right way,
# they should act like CODE refs.
use overload '&amp;{}' =&gt; sub { $_[0]-&gt;body }, fallback =&gt; 1;

# construction

sub wrap {
    my ( $class, @args ) = @_;

    unshift @args, 'body' if @args % 2 == 1;

    my %params = @args;
    my $code = $params{body};

    if (blessed($code) &amp;&amp; $code-&gt;isa(__PACKAGE__)) {
        my $method = $code-&gt;clone;
        delete $params{body};
        Class::MOP::class_of($class)-&gt;rebless_instance($method, %params);
        return $method;
    }
    elsif (!ref $code || 'CODE' ne reftype($code)) {
        confess "You must supply a CODE reference to bless, not (" . ($code || 'undef') . ")";
    }

    ($params{package_name} &amp;&amp; $params{name})
        || confess "You must supply the package_name and name parameters";

    my $self = $class-&gt;_new(\%params);

    weaken($self-&gt;{associated_metaclass}) if $self-&gt;{associated_metaclass};

    return $self;
}

sub _new {
    my $class = shift;

    return Class::MOP::Class-&gt;initialize($class)-&gt;new_object(@_)
        if $class ne __PACKAGE__;

    my $params = @_ == 1 ? $_[0] : {@_};

    return bless {
        'body'                 =&gt; $params-&gt;{body},
        'associated_metaclass' =&gt; $params-&gt;{associated_metaclass},
        'package_name'         =&gt; $params-&gt;{package_name},
        'name'                 =&gt; $params-&gt;{name},
        'original_method'      =&gt; $params-&gt;{original_method},
    } =&gt; $class;
}

## accessors

sub associated_metaclass { shift-&gt;{'associated_metaclass'} }

sub attach_to_class {
    my ( $self, $class ) = @_;
    $self-&gt;{associated_metaclass} = $class;
    weaken($self-&gt;{associated_metaclass});
}

sub detach_from_class {
    my $self = shift;
    delete $self-&gt;{associated_metaclass};
}

sub fully_qualified_name {
    my $self = shift;
    $self-&gt;package_name . '::' . $self-&gt;name;
}

sub original_method { (shift)-&gt;{'original_method'} }

sub _set_original_method { $_[0]-&gt;{'original_method'} = $_[1] }

# It's possible that this could cause a loop if there is a circular
# reference in here. That shouldn't ever happen in normal
# circumstances, since original method only gets set when clone is
# called. We _could_ check for such a loop, but it'd involve some sort
# of package-lexical variable, and wouldn't be terribly subclassable.
sub original_package_name {
    my $self = shift;

    $self-&gt;original_method
        ? $self-&gt;original_method-&gt;original_package_name
        : $self-&gt;package_name;
}

sub original_name {
    my $self = shift;

    $self-&gt;original_method
        ? $self-&gt;original_method-&gt;original_name
        : $self-&gt;name;
}

sub original_fully_qualified_name {
    my $self = shift;

    $self-&gt;original_method
        ? $self-&gt;original_method-&gt;original_fully_qualified_name
        : $self-&gt;fully_qualified_name;
}

sub execute {
    my $self = shift;
    $self-&gt;body-&gt;(@_);
}

# We used to go through use Class::MOP::Class-&gt;clone_instance to do this, but
# this was awfully slow. This method may be called a number of times when
# classes are loaded (especially during Moose role application), so it is
# worth optimizing. - DR
sub clone {
    my $self = shift;

    my $clone = bless { %{$self}, @_ }, blessed($self);
    weaken($clone-&gt;{associated_metaclass}) if $clone-&gt;{associated_metaclass};

    $clone-&gt;_set_original_method($self);

    return $clone;
}

1;

# ABSTRACT: Method Meta Object



=pod

=head1 NAME

Class::MOP::Method - Method Meta Object

=head1 VERSION

version 2.0603

=head1 DESCRIPTION

The Method Protocol is very small, since methods in Perl 5 are just
subroutines in a specific package. We provide a very basic
introspection interface.

=head1 METHODS

=over 4

=item B&lt;&lt; Class::MOP::Method-&gt;wrap($code, %options) &gt;&gt;

This is the constructor. It accepts a method body in the form of
either a code reference or a L&lt;Class::MOP::Method&gt; instance, followed
by a hash of options.

The options are:

=over 8

=item * name

The method name (without a package name). This is required if C&lt;$code&gt;
is a coderef.

=item * package_name

The package name for the method. This is required if C&lt;$code&gt; is a
coderef.

=item * associated_metaclass

An optional L&lt;Class::MOP::Class&gt; object. This is the metaclass for the
method's class.

=back

=item B&lt;&lt; $metamethod-&gt;clone(%params) &gt;&gt;

This makes a shallow clone of the method object. In particular,
subroutine reference itself is shared between all clones of a given
method.

When a method is cloned, the original method object will be available
by calling C&lt;original_method&gt; on the clone.

=item B&lt;&lt; $metamethod-&gt;body &gt;&gt;

This returns a reference to the method's subroutine.

=item B&lt;&lt; $metamethod-&gt;name &gt;&gt;

This returns the method's name

=item B&lt;&lt; $metamethod-&gt;package_name &gt;&gt;

This returns the method's package name.

=item B&lt;&lt; $metamethod-&gt;fully_qualified_name &gt;&gt;

This returns the method's fully qualified name (package name and
method name).

=item B&lt;&lt; $metamethod-&gt;associated_metaclass &gt;&gt;

This returns the L&lt;Class::MOP::Class&gt; object for the method, if one
exists.

=item B&lt;&lt; $metamethod-&gt;original_method &gt;&gt;

If this method object was created as a clone of some other method
object, this returns the object that was cloned.

=item B&lt;&lt; $metamethod-&gt;original_name &gt;&gt;

This returns the method's original name, wherever it was first
defined.

If this method is a clone of a clone (of a clone, etc.), this method
returns the name from the I&lt;first&gt; method in the chain of clones.

=item B&lt;&lt; $metamethod-&gt;original_package_name &gt;&gt;

This returns the method's original package name, wherever it was first
defined.

If this method is a clone of a clone (of a clone, etc.), this method
returns the package name from the I&lt;first&gt; method in the chain of
clones.

=item B&lt;&lt; $metamethod-&gt;original_fully_qualified_name &gt;&gt;

This returns the method's original fully qualified name, wherever it
was first defined.

If this method is a clone of a clone (of a clone, etc.), this method
returns the fully qualified name from the I&lt;first&gt; method in the chain
of clones.

=item B&lt;&lt; $metamethod-&gt;is_stub &gt;&gt;

Returns true if the method is just a stub:

  sub foo;

=item B&lt;&lt; $metamethod-&gt;attach_to_class($metaclass) &gt;&gt;

Given a L&lt;Class::MOP::Class&gt; object, this method sets the associated
metaclass for the method. This will overwrite any existing associated
metaclass.

=item B&lt;&lt; $metamethod-&gt;detach_from_class &gt;&gt;

Removes any associated metaclass object for the method.

=item B&lt;&lt; $metamethod-&gt;execute(...) &gt;&gt;

This executes the method. Any arguments provided will be passed on to
the method itself.

=item B&lt;&lt; Class::MOP::Method-&gt;meta &gt;&gt;

This will return a L&lt;Class::MOP::Class&gt; instance for this class.

It should also be noted that L&lt;Class::MOP&gt; will actually bootstrap
this module by installing a number of attribute meta-objects into its
metaclass.

=back

=head1 AUTHOR

Moose is maintained by the Moose Cabal, along with the help of many contributors. See L&lt;Moose/CABAL&gt; and L&lt;Moose/CONTRIBUTORS&gt; for details.

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2012 by Infinity Interactive, Inc..

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

=cut


__END__


</pre></body></html>