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:

tie my $obj, Tie::Constrained => sub { !$_[0] or $_[0]->isa('My::Frobnicator'); };
The first term is so that $obj can be undefined to break circular references.

Thank you much for the suggestions, I'll take them very seriously.

After Compline,
Zaxo


In reply to Re^2: Tie Me Up, Tie Me Down by Zaxo
in thread Tie Me Up, Tie Me Down by Zaxo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.