I recently thought wantarray would allow me to create a useful Mason component that would either return data in an array so I could format it element by element -- or return a string of that data pre-formatted. Not a good idea. In fact, Mason components already have a dual nature about their return values -- so I think that adding a decision split in the return value just compounds the problem even more. Here is the Mason code:
<%init> my @foo = $m->comp( '.foo' ); my $foo = $m->comp( '.foo' ); </%init> <%def .foo> <%init> my @array = qw(foo bar baz); return wantarray ? @array : join( '', map "$_<br/>", @array ); </%init> </%def>

Now, so far there are no problems. @foo does contain the unformatted elements and $foo contains a string of the elements pre-formatted. As long as I only use those variable in my "template" then i will have no issue:

<!-- correct --> <ol> %for (@foo) { <li><% $_ %></li> %} </ol> <!-- correct --> <p>foo = <% $foo %></p>

The problem comes when you try to take short cuts and call the component directly from your template. None of these do what you want:

<!-- incorrect --> <p>foo = <& .foo &></p> <!-- incorrect --> <p>foo = <% $m->comp( '.foo' ) %></p> <!-- incorrect --> <p>foo = <% $m->comp( '.foo' ) %></p>

The first is simply a wrong use of Mason* and the 2nd and 3rd return lists. This really is not a problem with wantarray so much as it is a trap that one thinks they can call Mason components in this manner -- and boy wouldn't it sure be great to utilize wantarray to make my call simple? Bzzzzzt. Here's the correct call:

<!-- correct --> <p>foo = <% scalar $m->comp( '.foo' ) %></p>
* this usage is like calling a function that prints data to STDOUT, and our comp does not do this

Convoluted and confusing. One is really better off defining a "shared" array and two separate subs. One that is a true Mason component that only returns data, and the other a true Mason component that only SPITS OUT data:

<%def .get_foo> <%init> my @array = qw(foo bar baz); return @array; </%init> </%def> <%def .display_foo> <% join( '', map "$_<br/>", $m->comp( '.get_foo' ) ) %> </%def> <!-- correct --> <ol> %for ($m->comp( '.get_foo' ) ) { <li><% $_ %></li> %} </ol> <!-- correct --> <& .display_foo &>

I hope this adds some useful points to your excellent meditation.

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

In reply to Re: Use of wantarray Considered Harmful (Mason) by jeffa
in thread Use of wantarray Considered Harmful by kyle

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.