Let's suppose I've created a library with the following API:

sub myfunc { my ($ref) = @_; # modify $ref somehow warn "Should be called in void context" if defined wantarray; }

The function does something, but does not returns anything useful. It returns nothing. I can add "return ;" but this does not change things - function returns nothing and I don't want it to be used in non-void context. Purpose of the last warning in function is simple: the function returns nothing, and if someone called it in scalar/list context, he's doing something wrong. So this warning adds more strictness to user's code:

examples of user's code:

example 1:

func1(); myfunc($data); func2();

everything ok - no warning

example 2:

func1(myfunc($data));

there will be the warning! user obvious did something wrong here!

example 3:

sub anotherfunc { # .. code here myfunc($data) } func1(anotherfunc())

there will be the warning! and indeed this code isn't sane.

So far everything going well, and the warning seems useful.

However, let's look on Catalyst now:

sub xxx : Local Args(0) { my ($self, $c, $s, $r, $p) = @_; $c->res->body( wantarray ); }

It's the Catalyst action, which return value is ignored, but it's called in list context.

Sometimes action return value is important (see "Any data returned from the action forwarded to, will be returned by the call to forward." in manual), sometimes no. But catalyst always call actions in list context.

Now the quesions:
(a) Is that good practice to create library, which warns for "defined wantarray" for functions, returning nothing?
(b) Are libraries like catalyst obligated to never call user's callback in non-void context if it's value is ignored?
I assume if (b)=no, then (a)=no.
Is there a convention for such kind of things? (or let me rephrase: if I am getting this warning when using my library with Catalyst, who's fault is that? the library's or Catalyst's)


In reply to Best practices for warnings about wrong context by vsespb

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.