my $attribute = shift;
my %params = @_;
- $params{places} = 2 if !defined($params{places});
-
- my $code = <<CODE;
-package ${package};
-
-sub ${attribute}_as_date {
- my \$self = shift;
-
- if (scalar \@_) {
- if (\$_[0]) {
- my (\$yy, \$mm, \$dd) = \$::locale->parse_date(\\\%::myconfig, \@_);
- \$self->${attribute}(DateTime->new(year => \$yy, month => \$mm, day => \$dd));
- } else {
- \$self->${attribute}(undef);
+ no strict 'refs';
+ *{ $package . '::' . $attribute . '_as_date' } = sub {
+ my ($self, $string) = @_;
+
+ if (@_ > 1) {
+ if ($string) {
+ my ($yy, $mm, $dd) = $::locale->parse_date(\%::myconfig, $string);
+ $self->$attribute(DateTime->new(year => $yy, month => $mm, day => $dd));
+ } else {
+ $self->$attribute(undef);
+ }
}
- }
-
- return \$self->${attribute} ? \$::locale->reformat_date({ dateformat => 'yy-mm-dd' }, \$self->${attribute}->ymd, \$::myconfig{dateformat}) : undef;
-}
-
-1;
-CODE
- eval $code;
- croak "Defining '${attribute}_as_number' failed: $EVAL_ERROR" if $EVAL_ERROR;
+ return $self->$attribute
+ ? $::locale->reformat_date(
+ { dateformat => 'yy-mm-dd' },
+ $self->${attribute}->ymd,
+ $::myconfig{dateformat}
+ )
+ : undef;
+ };
return 1;
}
$params{places} = 2 if !defined($params{places});
- my $code = <<CODE;
-package ${package};
+ no strict 'refs';
+ *{ $package . '::' . $attribute . '_as_number' } = sub {
+ my ($self, $string) = @_;
-sub ${attribute}_as_number {
- my \$self = shift;
+ $self->$attribute($::form->parse_amount(\%::myconfig, $string)) if @_ > 1;
- if (scalar \@_) {
- \$self->${attribute}(\$::form->parse_amount(\\\%::myconfig, \$_[0]));
- }
-
- return \$::form->format_amount(\\\%::myconfig, \$self->${attribute}, $params{places});
-}
-
-1;
-CODE
-
- eval $code;
- croak "Defining '${attribute}_as_number' failed: $EVAL_ERROR" if $EVAL_ERROR;
+ return $::form->format_amount(\%::myconfig, $self->$attribute, $params{places});
+ };
return 1;
}
$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;
}