2009-03-21 7 views
10

मैं अभी 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. 

किसी को भी पता है मूस में एक कस्टम ईमेल उप प्रकार बनाने के लिए कैसे?

मूस-संस्करण: 0.72 पर्ल संस्करण: 5.10.0, मंच: लिनक्स ubuntu 8.10

उत्तर

14

मैं भी मूस के लिए नया हूँ, लेकिन मुझे लगता है कि subtype के लिए, आप

जोड़ने की जरूरत
use Moose::Util::TypeConstraints; 
10

यहाँ एक मैं रसोई की किताब पहले से चुरा लिया है:

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

ईमेल :: मेल सत्यापन के लिए मान्य ++ # regexes बुराई – brunov

+4

@bruno है v - और ईमेल :: मान्य :: आरएफसी 822 सत्यापन के लिए रेगेक्स का उपयोग करता है। – converter42