in reply to pre-preprocess Moose args in constructor
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:sub BUILDARGS { my $class = shift; my %args = ref $_[0] ? %{$_[0]} : @_; $args{code} = uc $args{code} if exists $args{code}; return \%args; }
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";
|
|---|