develooper Front page | perl.moose | Postings from July 2011

howto: strict or relaxed type checking?

Thread Next
From:
Heiko Jansen
Date:
July 13, 2011 08:50
Subject:
howto: strict or relaxed type checking?
Message ID:
CACTXs=WFxE6_17nWdSe+m4wmtE2Y4jW7TC_HRF9Y9E2p814aYA@mail.gmail.com
Guys,

I´m trying to implement a Moose-based module for objects according to
some standard.

The standard tells us, that the objects may have a fixed set of
attributes with a defined structure.

There is, for example, a "date" attribute which should conform to "YYYY-MM-DD".
So as a simple first approach I defined an attribute "date" having as
type a subtype of Str which conforms to the regex
/\A(1[4-9]|20)\d\d-\d\d-\d\d\Z/. (Not very sophisticated, but probably
enough for this mail).

Now on one hand I want to make sure that the objects we create in our
software are valid.

On the other hand we see non-standard-compliant attribute values (e.g.
"12/01/1980" for "date") in incoming requests so that type checking
fails when parsing these requests.

However, I don´t want the parsing to fail in most cases, because the
quality of results computed over the inaccurate data will likely
degrade but not be completely useless.

So I´m looking for a way to switch on either strict or relaxed type
checking but I don´t know how.

The only solution I could think of was looking at a package-global
variable within the type checking code:

... where { if ($PKG::STRICTMODE) { $_ =~
/\A(1[4-9]|20)\d\d-\d\d-\d\d\Z/ } else {1} };

But I´d rather like to make the decision within an object scope like so:

Pack::MyClass->new( strict => 0 )->parse( $uri );

I haven´t got much experience using Moose and am hoping for someone to
share her/his experience on solutions considered good practice.

Thx.!
Heiko

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About