in reply to Re: Tie Me Up, Tie Me Down
in thread Tie Me Up, Tie Me Down
Regexp::Common is a very good partner for this tied technique - I had it in mind when I wrote Tie::Constrained. Here's an example of a subclass, Tie::Constrained::URI, where a tied variable is by default restricted to be a well-formed URI.
package Tie::Constrained::URI; use vars qw/@ISA/; use Regexp::Common 'URI'; use Tie::Constrained; @ISA = qw/Tie::Constrained/; sub validate { $_[0] =~ /^$RE{URI}$/ } 1; __DATA__ Usage: use Tie::Constrained::URI; my $href_ctl = tie my $href, 'Tie::Constrained::URI'; tie my $ftpref, Tie::Constrained::URI => sub { $_[0] =~ /^$RE{URI}{FTP}$/ }; $href_ctl can be used to change the test on the fly. $href_ctl->{'test'} = sub { $_[0] =~ /^$RE{URI}{HTTP}$/ } if $href =~ /^$RE{URI}{HTTP}$/;
As it stands, I think that Params::Validate compatibility is for the future, but it's a very good idea. I'll probably need to rename my validate class method again.
Many of the simpler things Params::Validate does for objects can be imitated with scalar Tie::Constrained as it is:
The first term is so that $obj can be undefined to break circular references.tie my $obj, Tie::Constrained => sub { !$_[0] or $_[0]->isa('My::Frobnicator'); };
Thank you much for the suggestions, I'll take them very seriously.
After Compline,
Zaxo
|
---|