vsespb has asked for the wisdom of the Perl Monks concerning the following question:
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)
|
---|