2014-02-25 8 views
2

두 가지 구성 요소가 있습니다. test.mc from 구성 요소 name.mi에 정의 된 두 가지 메소드를 호출해야합니다.Mason 구성 요소에 정의 된 여러 메서드를 호출하는 방법?

/test.mc 실행 중 컴포넌트 /name.mi

<%class> 
    has 'name'; 
</%class> 

<%method text> 
<pre>Some text here</pre> 
</%method> 

<%method showname> 
NAME: <% $.name %> 
</%method> 

성분 /test.mc

<%init> 
     my $namecomp = $m->load('name.mi', name=>'john'); 
</%init> 
<% $namecomp->text %> 
<% $namecomp->showname %> 

:

  • $namecomp->text 작동 방법. 이 오류주는
  • $namecomp->showname NOT 작품 :

Can't use string ("MC0::name_mi") as a HASH ref while "strict refs" in use at accessor MC0::name_mi::name (defined at /.../testpoet/comps/name.mi line 2) line 5

질문 :

  • 사람이 어떻게 제대로 $m->load($path)를 사용하는 나에게 예를 보일 수 있는가?
  • showname method에서 $.name에 액세스 할 수없는 이유는 무엇입니까? name.mi 구성 요소에 정의 된 여러 메서드를 호출하는 방법은 무엇입니까?

예를 들어, (schematicaly)로 다음 쓸 수있는 순수한 펄에서 어떤 것을 달성 할 함께 :

package Myapp::Name; 
has 'name'; 
method text() { 
    print "some text"; 
} 
method showname { 
    print "Name: " . $self->name(); 
} 

과로 사용 :

my $namecomp = Myapp::Name->new(name => 'John'); 
$namecomp->text; 
$namecomp->showname; 

답변

3

사용 construct 대신 load

부터 perldoc Mason::Request :

load (path) 
     Makes the component path absolute if necessary, and calls Interp load 
     to load the component class associated with the path. 

...

construct (path[, params ...]) 
     Constructs and return a new instance of the component designated by 
     path params, if any, are passed to the constructor. Throws an error 
     if path does not exist. 

로드는 생성하는 동안 사용 가능한 개체를 반환하지 않습니다.

다음은 /test.mc에 나를 위해 일한 :

<%init> 
     my $namecomp = $m->construct('name.mi', name=>'john'); 
</%init> 
<% $namecomp->text %> 
<% $namecomp->showname %> 
+0

가 대단히 감사합니다! :) – kobame

관련 문제