perlpreparemason

Perl - When <%method PREPARE> gets called


I am new to Perl Mason.

I came across this with the suggestion that service calls should be put inside PREPARE block. But when I placed my service calls inside it, seems like the code inside it is never getting executed itself.

<%method PREPARE>

Kindly suggest what is the above block for and its usage.


Solution

  • From the Mason Manual:

    The base component class, Mason::Component, has but a few built-in methods: handle, render, wrap, main, m, and cmeta.

    The main method contains the mix of HTML and Perl in the main part of the component.

    You can add other methods that output HTML via the section; these methods automatically have access to $self and $m.

    <%method leftcol>
      <table><tr>
        <td><% $foo %></td>
        ...
      </tr></table>
    </%method>
    
    ...
    
    <% # call leftcol method and insert HTML here %>
    <% $.leftcol %>
    

    Which means you are declaring a method named PREPARE without any argument lists by <%method PREPARE> and after writing the method body you will end it using </%method>.

    And, later somewhere you will call it using <% $.PREPARE %>.For more info refer the Mason Manual.