当前位置:  开发笔记 > 编程语言 > 正文

你如何在穆斯创建子类型?

如何解决《你如何在穆斯创建子类型?》经验,为你挑选了2个好方法。

我刚刚开始使用驼鹿.

我正在创建一个简单的通知对象,并希望检查输入是否为"电子邮件"类型.(暂时忽略简单的正则表达式匹配).

从文档中我相信它应该类似于以下代码:

# --- contents of message.pl --- #
package Message;
use Moose;

subtype 'Email' => as 'Str' => where { /.*@.*/ } ;

has 'subject' => ( isa => 'Str', is => 'rw',);
has 'to'      => ( isa => 'Email', is => 'rw',);

no Moose; 1;
#############################
package main;

my $msg = Message->new( 
    subject => 'Hello, World!', 
    to => 'coolkids@example.com' 
);  
print $msg->{to} . "\n";

但是我收到以下错误:

String found where operator expected at message.pl line 5, near "subtype 'Email'"
    (Do you need to predeclare subtype?)
String found where operator expected at message.pl line 5, near "as 'Str'"
    (Do you need to predeclare as?)
syntax error at message.pl line 5, near "subtype 'Email'"
BEGIN not safe after errors--compilation aborted at message.pl line 10.

任何人都知道如何在Moose中创建自定义电子邮件子类型?

Moose版本:0.72 perl-version:5.10.0,平台:linux-ubuntu 8.10



1> oylenshpeegu..:

我也是Moose的新手,但我认为对于子类型,你需要添加

use Moose::Util::TypeConstraints;



2> singingfish..:

这是我之前从食谱中偷走的一个:

package MyPackage;
use Moose;
use Email::Valid;
use Moose::Util::TypeConstraints;

subtype 'Email'
   => as 'Str'
   => where { Email::Valid->address($_) }
   => message { "$_ is not a valid email address" };

has 'email'        => (is =>'ro' , isa => 'Email', required => 1 );


@brunov - &Email :: Valid :: rfc822使用正则表达式进行验证.
推荐阅读
牛尾巴2010
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有