From aa86663451de1406e59234c537fcc7533dbfa2a3 Mon Sep 17 00:00:00 2001 From: Hauke D Date: Wed, 10 Apr 2019 18:22:54 +0200 Subject: [PATCH] Added a note on "sub" return values --- using.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/using.md b/using.md index 6975f0e..8b4a06b 100644 --- a/using.md +++ b/using.md @@ -529,7 +529,7 @@ The exceptions are Perl `sub`s and `WebPerl::JSObject`s. - Perl arrayrefs become JavaScript arrays (deep copy) - Perl hashrefs become JavaScript objects (deep copy) -- Perl coderefs become JavaScript functions - +- Perl coderefs become JavaScript functions² - **Warning:** please see the discussion in ["Memory Management and Anonymous `sub`s"](#memory-management-and-anonymous-subs)! - Perl `WebPerl::JSObject`s become references to the wrapped JavaScript objects @@ -550,6 +550,13 @@ future be extended to return more than that, similar to `WebPerl::js()`, and then the passing of Perl values to JavaScript could be accomplished differently as well. +² **Remember** that Perl `sub`s without an explicit `return` statement will +implicitly return the value of the last statement (if it is an expression). +This value will in turn be passed to JavaScript, which may be inefficient +if this value is not needed, and it may cause errors if the return value cannot +be encoded to JavaScript. Therefore it is recommended to get into the +habit of adding an explicit `return;` at the end of `sub`s passed to JS. + ### `js_new()` This function is a convenience function for calling JavaScript's `new`.