use Mouse::Util qw(:meta); # enables strict and warnings
use warnings FATAL => 'recursion';
-use Mouse::Meta::Method::Constructor; # for slot access
+
+sub _inline_slot{
+ my(undef, $self_var, $attr_name) = @_;
+ return sprintf '%s->{q{%s}}', $self_var, $attr_name;
+}
sub _generate_accessor_any{
my($method_class, $type, $attribute, $class) = @_;
- my $c = 'Mouse::Meta::Method::Constructor';
-
- my $key = $attribute->name;
+ my $name = $attribute->name;
my $default = $attribute->default;
my $constraint = $attribute->type_constraint;
my $builder = $attribute->builder;
my $compiled_type_constraint = defined($constraint) ? $constraint->_compiled_type_constraint : undef;
- my $instance = '$_[0]';
- my $slot = $c->_inline_get_slot($instance, $key);;
+ my $self = '$_[0]';
+ my $slot = $method_class->_inline_slot($self, $name);;
- my $accessor = sprintf(<<'END_SUB_START', $class->name, __LINE__, $type, $key, __FILE__);
-package %s;
-#line %d "%s-accessor for %s (%s)
-sub {
-END_SUB_START
+ my $accessor = sprintf(qq{package %s;\n#line 1 "%s-accessor for %s (%s)"\n}, $class->name, $type, $name, __FILE__)
+ . "sub {\n";
if ($type eq 'rw' || $type eq 'wo') {
if($type eq 'rw'){
}
else{ # writer
$accessor .=
- 'if(@_ < 2){ Carp::confess("Not enough arguments for the writer of '.$key.'") }'.
+ 'if(@_ < 2){ Carp::confess("Not enough arguments for the writer of '.$name.'") }'.
'{' . "\n";
}
# this setter
$accessor .= 'return ' if !$is_weak && !$trigger && !$should_deref;
- $accessor .= $c->_inline_set_slot($instance, $key, $value) . ";\n";
+ $accessor .= "$slot = $value;\n";
if ($is_weak) {
- $accessor .= $c->_inline_weaken_slot($instance, $key) ." if ref $slot;\n";
+ $accessor .= "Scalar::Util::weaken($slot) if ref $slot;\n";
}
if ($trigger) {
- $accessor .= '$trigger->('.$instance.', '.$value.');' . "\n";
+ $accessor .= '$trigger->('.$self.', '.$value.');' . "\n";
}
$accessor .= "}\n";
my $value;
if (defined $builder){
- $value = "$instance->\$builder()";
+ $value = "$self->\$builder()";
}
elsif (ref($default) eq 'CODE'){
- $value = "$instance->\$default()";
+ $value = "$self->\$default()";
}
else{
$value = '$default';
}
- $accessor .= sprintf "if(!%s){\n", $c->_inline_has_slot($instance, $key);
+ $accessor .= "if(!exists $slot){\n";
if($should_coerce){
- $value = "\$constraint->coerce($value)";
+ $accessor .= "$slot = \$constraint->coerce($value)";
}
elsif(defined $constraint){
$accessor .= "my \$tmp = $value;\n";
$accessor .= "\$compiled_type_constraint->(\$tmp)";
$accessor .= " || \$attribute->_throw_type_constraint_error(\$tmp, \$constraint);\n";
- $value = '$tmp';
+ $accessor .= "$slot = \$tmp;\n";
+ }
+ else{
+ $accessor .= "$slot = $value;\n";
}
-
- $accessor .= $c->_inline_set_slot($instance, $key, $value) .";\n";
-
if ($is_weak) {
- $accessor .= $c->_inline_weaken_slot($instance, $key) . " if ref $slot;\n";
+ $accessor .= "Scalar::Util::weaken($slot) if ref $slot;\n";
}
$accessor .= "}\n";
}
package Mouse::Meta::Method::Constructor;
use Mouse::Util qw(:meta); # enables strict and warnings
-sub _inline_create_instance {
- my(undef, $class_expr) = @_;
- return "bless {}, $class_expr";
-}
-
-sub _inline_slot {
+sub _inline_slot{
my(undef, $self_var, $attr_name) = @_;
return sprintf '%s->{q{%s}}', $self_var, $attr_name;
}
-sub _inline_has_slot {
- my($class, $self_var, $attr_name) = @_;
-
- return sprintf 'exists(%s)', $class->_inline_slot($self_var, $attr_name);
-}
-
-sub _inline_get_slot {
- my($class, $self_var, $attr_name) = @_;
-
- return $class->_inline_slot($self_var, $attr_name);
-}
-
-sub _inline_set_slot {
- my($class, $self_var, $attr_name, $rvalue) = @_;
-
- return $class->_inline_slot($self_var, $attr_name) . " = $rvalue";
-}
-
-sub _inline_weaken_slot {
- my($class, $self_var, $attr_name) = @_;
-
- return sprintf 'Scalar::Util::weaken(%s)', $class->_inline_slot($self_var, $attr_name);
-}
-
sub _generate_constructor {
my ($class, $metaclass, $args) = @_;
+ my $associated_metaclass_name = $metaclass->name;
+
my @attrs = $metaclass->get_all_attributes;
- my $init_attrs = $class->_generate_processattrs($metaclass, \@attrs);
- my $buildargs = $class->_generate_BUILDARGS($metaclass);
my $buildall = $class->_generate_BUILDALL($metaclass);
+ my $buildargs = $class->_generate_BUILDARGS($metaclass);
+ my $processattrs = $class->_generate_processattrs($metaclass, \@attrs);
my @checks = map { $_ && $_->_compiled_type_constraint }
map { $_->type_constraint } @attrs;
- my $class_name = $metaclass->name;
- my $source = sprintf(<<'END_CONSTRUCTOR', $class_name, __LINE__, __FILE__, $class_name, $buildargs, $class->_inline_create_instance('$class'), $init_attrs, $buildall);
-package %s;
-#line %d "constructor of %s (%s)"
- sub {
- my $class = shift;
- return $class->Mouse::Object::new(@_)
- if $class ne __PACKAGE__;
+ my $source = sprintf("#line %d %s\n", __LINE__, __FILE__).<<"...";
+ sub \{
+ my \$class = shift;
+ return \$class->Mouse::Object::new(\@_)
+ if \$class ne q{$associated_metaclass_name};
# BUILDARGS
- %s;
- # create instance
- my $instance = %s;
+ $buildargs;
+ my \$instance = bless {}, \$class;
# process attributes
- %s;
+ $processattrs;
# BUILDALL
- %s;
- return $instance;
+ $buildall;
+ return \$instance;
}
-END_CONSTRUCTOR
+...
#warn $source;
my $code;
my $e = do{
my $is_weak_ref = $attr->is_weak_ref;
my $need_coercion;
- my $instance = '$instance';
- my $instance_slot = $method_class->_inline_get_slot($instance, $key);
+ my $instance_slot = $method_class->_inline_slot('$instance', $key);
my $attr_var = "\$attrs[$index]";
my $constraint_var;
$post_process .= " or $attr_var->_throw_type_constraint_error($instance_slot, $constraint_var);\n";
}
if($is_weak_ref){
- $post_process .= $method_class->_inline_weaken_slot($instance, $key) . " if ref $instance_slot;\n";
+ $post_process .= "Scalar::Util::weaken($instance_slot) if ref $instance_slot;\n";
}
if (defined $init_arg) {
$value = "$constraint_var->coerce($value)";
}
- $code .= $method_class->_inline_set_slot($instance, $key, $value) . ";\n";
+ $code .= "$instance_slot = $value;\n";
$code .= $post_process;
if ($attr->has_trigger) {
$value = "$constraint_var->coerce($value)";
}
- $code .= $method_class->_inline_set_slot($instance, $key, $value) . ";\n";
+ $code .= "$instance_slot = $value;\n";
if($is_weak_ref){
- $code .= $method_class->_inline_weaken_slot($instance, $key) . ";\n";
+ $code .= "Scalar::Util::weaken($instance_slot);\n";
}
}
}