http://www.perlmonks.org?node_id=967208


in reply to Re^2: Checking Wrong Condition
in thread Checking Wrong Condition

Subroutine can return both scalar and list context values, so return a list context values as in our case here. One can use wantarray() to tell which context as stated in the perldoc -f wantarray documentation, and I quote "Returns true if the context of the currently executing subroutine or "eval" is looking for a list value. Returns false if the context is looking for a scalar. Returns the undefined value if the context is looking for no value (void context)."

E.g:

my @values=get_values(); # subroutine called print @values; ## print 12345 not 1,2,3,4,5 sub get_values{ my @init_val=grep $_=>1..5; if(wantarray){ # test list context return @init_val; }else{return join",",split} }