如何在 Moose 中创建子类型?

发布于 2024-07-14 23:49:49 字数 1259 浏览 5 评论 0原文

我刚刚开始使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╰沐子 2024-07-21 23:49:50

我也是 Moose 新手,但我认为对于 subtype,您需要添加

use Moose::Util::TypeConstraints;

I am new to Moose as well, but I think for subtype, you need to add

use Moose::Util::TypeConstraints;
云淡风轻 2024-07-21 23:49:50

这是我之前从食谱中偷来的:

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 );

Here's one I stole from the cookbook earlier:

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 );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文