If you want to use wantarray to tell you the context of an entity within a larger expression (which is what you seem to be trying to do), you have to call wantarray from within a sub, inserted into that larger expression.
So the following two examples will do what you're seeking:
print bleah(), "\n"; sub context { return wantarray() ? "List" : "Scalar"; } sub bleah { return ( "Context: " . context() );
...or if you're trying your hand at obfu...
print bleah(), "\n"; sub bleah { return ( "Context: " . &{sub { wantarray() ? "List" : "Scalar" }}() ); }
The second example creates an anonymous sub in which wantarray may reside, and then dereferences the anonymous sub, executing it within the concatenation expression.
Dave
In reply to Re: wantarray - surprise behaviour
by davido
in thread wantarray - surprise behaviour
by shemp
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |