Attr Helper umgeschrieben auf dnamisch registrierte coderefs.
[kivitendo-erp.git] / SL / DB / Helpers / AttrPercent.pm
index 24f8164..e4c44ae 100644 (file)
@@ -12,24 +12,14 @@ sub define {
 
   $params{places} = 2 if !defined($params{places});
 
-  my $code        = <<CODE;
-package ${package};
+  no strict 'refs';
+  *{ $package . '::' . $attribute . '_as_percent' } = sub {
+    my ($self, $string) = @_;
 
-sub ${attribute}_as_percent {
-  my \$self = shift;
+    $self->$attribute($::form->parse_amount(\%::myconfig, $string) / 100) if @_ > 1;
 
-  if (scalar \@_) {
-    \$self->${attribute}(\$::form->parse_amount(\\\%::myconfig, \$_[0]) / 100);
-  }
-
-  return \$::form->format_amount(\\\%::myconfig, 100 * \$self->${attribute}, $params{places});
-}
-
-1;
-CODE
-
-  eval $code;
-  croak "Defining '${attribute}_as_number' failed: $EVAL_ERROR" if $EVAL_ERROR;
+    return $::form->format_amount(\%::myconfig, 100 * $self->$attribute, $params{places});
+  };
 
   return 1;
 }