Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^4: How to enforce void context in Reply?

by Anonymous Monk
on May 19, 2015 at 12:03 UTC ( [id://1127114]=note: print w/replies, xml ) Need Help??


in reply to Re^3: How to enforce void context in Reply?
in thread How to enforce void context in Reply?

If there is a sub foo that uses the above method of detecting when it is called in void context so that it can do less work; then that would be a reason the caller of sub foo (docdurdee) might want to enforce void context on it.

Replies are listed 'Best First'.
Re^5: How to enforce void context in Reply?
by AnomalousMonk (Archbishop) on May 19, 2015 at 14:46 UTC

    But doesn't just calling  foo() in void context
        foo();
    enforce void context upon the invocation of foo()? What need for anything else?

    Is it ever possible to get a value directly returned from a subroutine invoked in void context, whether "enforced" or "natural" (as in the example above)? (Of course, it's always possible to indirectly return values from subroutines invoked in void context.)


    Give a man a fish:  <%-(-(-(-<

      What need for anything else?

      True, but it can still happen by accident that a sub is executed in non-void context when the programmer wanted it in void context, as shown above in sub calls_it_1. Also docdurdee mentioned a REPL, so my guess would be that the REPL puts every line entered at the prompt into some non-void context.

        All the more reason to avoid implicit return statements.

        sub Sa { foo(); # implicit return: foo() called in Sa() invocation context +. } sub Sb { return foo(); # explicit return: foo() called in Sb() invocation +context. } sub Sc { foo(); # foo() called in void context. no question. return; }
        IMHO, the Sa() form should always be avoided.

        And also IMH (but very emphatic!) O, one should certainly avoid weird "implicit return of 1" statements which are no more than maintainence pitfalls.


        Give a man a fish:  <%-(-(-(-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1127114]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2024-04-23 19:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found