There are several ways to do this, depending on how much validation you want to add. A lightweight method without any validation is to add a
BUILDARGS routine to your class (see
Moose::Manual::Construction):
sub BUILDARGS {
my $class = shift;
my %args = ref $_[0] ? %{$_[0]} : @_;
$args{code} = uc $args{code} if exists $args{code};
return \%args;
}
A more idiomatic method is to add a Moose type for the currency code, check for its validity, and then allow lowercase input by converting it under the hood by Moose coercion. Here's a complete example:
package UpperCaseDemo;
use Moose;
use Moose::Util::TypeConstraints;
with 'MooseX::Getopt';
subtype 'CurrencyCode'
=> as 'Str'
=> where { /^[A-Z]{3}$/ }
=> message { 'Currency codes should be three characters' },
;
coerce 'CurrencyCode'
=> from 'Str'
=> via { uc }
;
has 'code' => (
is => 'ro', isa => 'CurrencyCode',
coerce => 1,
);
1;
# -----------
# Usage: lookup --code abc or lookup --code ABC
#
package main;
my $cc = UpperCaseDemo->new_with_options();
print "code is ", $cc->code // 'not set', "\n";
BTW: I don't think that triggers are supposed to be allowed to change readonly attributes. Where did you read that?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.