如何在 Moose 中创建子类型?
我刚刚开始使用 Moose。
我正在创建一个简单的通知对象,并想检查输入是否为“电子邮件”类型。 (暂时忽略简单的正则表达式匹配)。
从文档中我相信它应该类似于以下代码:
# --- 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 => '[email protected]'
);
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 版本:5.10.0, 平台:linux-ubuntu 8.10
I'm just starting to use Moose.
I'm creating a simple notification object and would like to check inputs are of an 'Email' type. (Ignore for now the simple regex match).
From the documentation I believe it should look like the following code:
# --- 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 => '[email protected]'
);
print $msg->{to} . "\n";
but I get the following errors:
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.
Anyone know how to create a custom Email subtype in Moose?
Moose-version : 0.72
perl-version : 5.10.0,
platform : linux-ubuntu 8.10
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我也是 Moose 新手,但我认为对于 subtype,您需要添加
I am new to Moose as well, but I think for subtype, you need to add
这是我之前从食谱中偷来的:
Here's one I stole from the cookbook earlier: