This method works great! I was very close to getting coercion to work, but somehow my substitution returned '2' every time. Thanks all for the help

Steve

Something along the lines of the following should work:

package Types;

use Moose::Util::TypeConstraints;

# note, need a type here, as otherwise coersions won't work if done as
a subtype of 'Str'
type "Types.pcr"
  =>  where { defined $_ and $_ =~ /^\d+$/ };

coerce "Types.pcr"
  =>  from "Str"
  =>  via { my $v = shift; $v =~ s/-//g; $v };

package UsageDetail;

use Moose;

# Note the 'coerce' flag and isa here
has pcr =>  (
  is =>  "rw",
  isa =>  "Types.pcr",
  coerce =>  1
);

package main;

my $dashful = UsageDetail->new( pcr =>  "9-8-7-6-5-4-3-2-1" );

my $dashless = UsageDetail->new( pcr =>  "123456789" );

print "Dashful: " . $dashful->pcr . "\n";
print "Dashless: " .$dashless->pcr . "\n"

-
The above gives:

Dashful: 987654321
Dashless: 123456789

On Fri, Feb 26, 2010 at 2:21 PM, Steve<[email protected]>  wrote:
 I have been attempting to solve the following problem:

 I have a class, 'UsageDetail' which takes a CSV phone call record and
 inserts it into my database.  One of the attributes, 'WirelessNumber' has
 dashes in it, ie: '989-555-1212'.  I don't want to store the dashes in the
 db.  I've rtfm over and over, but I haven't been successful in storing
 without the dashes.  I've tried subtypes, but that didn't work.  BTW, the
 values passed in to my constructor are not ONLY the 3-3-4 digit format,
 sometimes the wireless number is 2 digits, and my WirelessNumber attr. isa
 'Str' currently.  Also, I can't modify the value passed into my constructor,
 as it is used for many different tables.  Any suggestions are greatly
 appreciated.

 Steve


No virus found in this incoming message.
Checked by AVG - www.avg.com 
Version: 9.0.733 / Virus Database: 271.1.1/2711 - Release Date: 02/26/10 
02:34:00

Reply via email to