2014-02-06 4 views
3

무언가 빌더를 호출하지 않고 속성 값을 인쇄하면서 무스 객체의 속성을 반복하려고합니다 (속성 값이 존재하는 경우 인쇄 함에도 불구하고).무스 속성이 있습니다.

for my $attr ($object->meta->get_all_attributes) { 
    my $name = $attr->name; 

    # Lazy attributes that have not already been generated will not 
    # exist in the object hash. 
    next unless exists $object->{$name} 

    my $value = $object->$name; 
    print $value; 
} 

이 속성 값이 무스 클래스 자체를 수정하지 않고, 존재하는 경우에 저에게 말할 것이다 무스를 사용하여 객체를 검사하는 방법이 있나요 :

내 코드는 지금까지처럼 보인다?

즉 어떤 도움과 배려 :

답변

3

Class::MOP::ClassClass::MOP::Attribute 당신을 가리킬 것이다 Moose::Meta::Class 문서를 읽기위한

감사 위의 코드에서 "존재 다음하지 않는 한"라인에 더 우아한 대안.

foreach my $attr ($object->meta->get_all_attributes) { 
    my $name = $attr->name; 

    next unless $attr->has_value($object); 

    # Or, perhaps get_value(), depending on your requirements. 
    say $attr->get_raw_value($object); 
} 
:

당신은 다음과 같은 코드 뭔가를 쓸 수 있습니다

관련 문제