<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::Constructor;
our $VERSION = '2.2013';

use strict;
use warnings;

use Scalar::Util 'blessed', 'weaken';
use Try::Tiny;

use parent 'Class::MOP::Method::Inlined';

sub new {
    my $class   = shift;
    my %options = @_;

    (blessed $options{metaclass} &amp;&amp; $options{metaclass}-&gt;isa('Class::MOP::Class'))
        || $class-&gt;_throw_exception( MustSupplyAMetaclass =&gt; params =&gt; \%options,
                                                    class  =&gt; $class
                          )
            if $options{is_inline};

    ($options{package_name} &amp;&amp; $options{name})
        || $class-&gt;_throw_exception( MustSupplyPackageNameAndName =&gt; params =&gt; \%options,
                                                            class  =&gt; $class
                          );

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

    # we don't want this creating
    # a cycle in the code, if not
    # needed
    weaken($self-&gt;{'associated_metaclass'});

    $self-&gt;_initialize_body;

    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 {
        # inherited from Class::MOP::Method
        body                 =&gt; $params-&gt;{body},
        # associated_metaclass =&gt; $params-&gt;{associated_metaclass}, # overridden
        package_name         =&gt; $params-&gt;{package_name},
        name                 =&gt; $params-&gt;{name},
        original_method      =&gt; $params-&gt;{original_method},

        # inherited from Class::MOP::Generated
        is_inline            =&gt; $params-&gt;{is_inline} || 0,
        definition_context   =&gt; $params-&gt;{definition_context},

        # inherited from Class::MOP::Inlined
        _expected_method_class =&gt; $params-&gt;{_expected_method_class},

        # defined in this subclass
        options              =&gt; $params-&gt;{options} || {},
        associated_metaclass =&gt; $params-&gt;{metaclass},
    }, $class;
}

## accessors

sub options              { (shift)-&gt;{'options'}              }
sub associated_metaclass { (shift)-&gt;{'associated_metaclass'} }

## method

sub _initialize_body {
    my $self        = shift;
    my $method_name = '_generate_constructor_method';

    $method_name .= '_inline' if $self-&gt;is_inline;

    $self-&gt;{'body'} = $self-&gt;$method_name;
}

sub _eval_environment {
    my $self = shift;
    return $self-&gt;associated_metaclass-&gt;_eval_environment;
}

sub _generate_constructor_method {
    return sub { Class::MOP::Class-&gt;initialize(shift)-&gt;new_object(@_) }
}

sub _generate_constructor_method_inline {
    my $self = shift;

    my $meta = $self-&gt;associated_metaclass;

    my @source = (
        'sub {',
            $meta-&gt;_inline_new_object,
        '}',
    );

    warn join("\n", @source) if $self-&gt;options-&gt;{debug};

    my $code = try {
        $self-&gt;_compile_code(\@source);
    }
    catch {
        my $source = join("\n", @source);
        $self-&gt;_throw_exception( CouldNotEvalConstructor =&gt; constructor_method =&gt; $self,
                                                    source             =&gt; $source,
                                                    error              =&gt; $_
                       );
    };

    return $code;
}

1;

# ABSTRACT: Method Meta Object for constructors

__END__

=pod

=encoding UTF-8

=head1 NAME

Class::MOP::Method::Constructor - Method Meta Object for constructors

=head1 VERSION

version 2.2013

=head1 SYNOPSIS

  use Class::MOP::Method::Constructor;

  my $constructor = Class::MOP::Method::Constructor-&gt;new(
      metaclass =&gt; $metaclass,
      options   =&gt; {
          debug =&gt; 1, # this is all for now
      },
  );

  # calling the constructor ...
  $constructor-&gt;body-&gt;execute($metaclass-&gt;name, %params);

=head1 DESCRIPTION

This is a subclass of L&lt;Class::MOP::Method&gt; which generates
constructor methods.

=head1 METHODS

=over 4

=item B&lt;&lt; Class::MOP::Method::Constructor-&gt;new(%options) &gt;&gt;

This creates a new constructor object. It accepts a hash reference of
options.

=over 8

=item * metaclass

This should be a L&lt;Class::MOP::Class&gt; object. It is required.

=item * name

The method name (without a package name). This is required.

=item * package_name

The package name for the method. This is required.

=item * is_inline

This indicates whether or not the constructor should be inlined. This
defaults to false.

=back

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

Returns a boolean indicating whether or not the constructor is
inlined.

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

This returns the L&lt;Class::MOP::Class&gt; object for the method.

=back

=head1 AUTHORS

=over 4

=item *

Stevan Little &lt;stevan.little@iinteractive.com&gt;

=item *

Dave Rolsky &lt;autarch@urth.org&gt;

=item *

Jesse Luehrs &lt;doy@tozt.net&gt;

=item *

Shawn M Moore &lt;code@sartak.org&gt;

=item *

×™×•×‘×œ ×§×•×’'×ž×Ÿ (Yuval Kogman) &lt;nothingmuch@woobling.org&gt;

=item *

Karen Etheridge &lt;ether@cpan.org&gt;

=item *

Florian Ragwitz &lt;rafl@debian.org&gt;

=item *

Hans Dieter Pearcey &lt;hdp@weftsoar.net&gt;

=item *

Chris Prather &lt;chris@prather.org&gt;

=item *

Matt S Trout &lt;mst@shadowcat.co.uk&gt;

=back

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2006 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
</pre></body></html>