Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Help with Moose subtype

by neilwatson (Curate)
on Sep 18, 2012 at 01:34 UTC ( #994165=perlquestion: print w/ replies, xml ) Need Help??
neilwatson has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Sisters and Brothers, I'm learning Moose. I'm trying to set a subtype type but, having trouble. Please consider the following:
subtype 'cf_class', => as 'Str', => where { m/a-zA-Z0-9/ }, => message { 'Not a valid cf class' }; has 'class' => { is => 'rw', isa => 'cf_class' };
I'm getting this error:
Usage: has 'name' => ( key => value, ... ) at /home/neil/perl5/lib/per +l5/x86_64-linux-thread-multi/Moose.pm line 74. Moose::has('Moose::Meta::Class=HASH(0x2b9fe98)', 'class', 'HAS +H(0x2a9ef98)') called at /home/neil/perl5/lib/perl5/x86_64-linux-thre +ad-multi/Moose/Exporter.pm line 382
My desire is to ensure that the parameter 'class' is limited to /a-zA-Z0-9/. What have I done wrong?

Neil Watson
watson-wilson.ca

Comment on Help with Moose subtype
Select or Download Code
Re: Help with Moose subtype
by mbethke (Hermit) on Sep 18, 2012 at 02:13 UTC
    You put the parameters to "has" in curlies instead of parentheses :)
Re: Help with Moose subtype
by tobyink (Abbot) on Sep 18, 2012 at 07:08 UTC

    mbethke is correct.

    You've also got a problem with your regular expression. Currently, the following string passes your type constraint, when I assume from your description that you expected it to fail:

    $#!T

    The regexp I think you want is: /^[A-Za-z0-9]*$/.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://994165]
Approved by kevbot
Front-paged by Tanktalus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2015-07-02 06:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (30 votes), past polls