Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Passing arguments to event handlers in WxPerl

by Laurent_R (Canon)
on Sep 18, 2014 at 22:30 UTC ( [id://1101116]=note: print w/replies, xml ) Need Help??


in reply to Passing arguments to event handlers in WxPerl

I'm familiar with passing pointers in C. I think that "aliasing" may be analogous to that, but I don't know how it's done in Perl.

Sort of, but not quite. Something really similar to passing pointers in C is to pass references to variables or data structures (or to subroutines or other things). When using a function in a module that uses TK (or for many other purposes, for that matter), you might end up writing something similar to this:

my_function( [\&callback_func, $cb_param1, $cb_param2], $arg2, $arg3);
Here, three parameters are passed to my_function: a reference to a data structure to be explained shortly and two additional arguments, $arg2 and $arg3. The first argument is a reference to an array that contains three elements: a reference to a callback subroutine (defined in the user program, but used by the my_function subroutine within the module), and two arguments passed to that callback subroutine. Of course, the my_function has to be built in order to be able to process these three arguments and especially the three items contained in the first argument.

Aliasing is somewhat different: it is an internal Perl process whereby the data is essentially passed by reference, so that if your function modifies its incoming argument, the variable that was passed to the function will be modified within the caller function. The main reason for doing that is that if you pass a large array, Perl does not make a copy of the array, but just provides to the called function an alias to that array. This can be significantly faster. The consequence is that if you modify that array, it will be modified in the caller function. For that reason, it is often considered good policy to start a subroutine with a copy of the arguments into local variables, to avoid to inadvertently modify the caller's execution environment (and obtain a reliable pass-by-value type of semantic). It is the responsibility of the programmer to decide whether or not to do it, depending on the desired effect. To be complete (but without going into gory details), I should add that aliasing also occurs in other circumstances, such as the use of for loops or expression modifiers, map and grep constructs, etc.

But aliasing an array passed to a subroutine (or to a for loop) is done automatically by Perl, you have relatively little control on it. Whereas passing an actual reference gives the developer full control on what happens. The most obvious example is that if you pass, say, two arrays to a subroutine, the subroutine will see only one @_ array containing a flattened list of the values of the two arrays, with no means of distinguishing between the values from array1 and from array2. Sometimes, it is OK, sometimes not. In contrast, if you pass a reference to each array, the called subroutine can fully distinguish the two arrays.

Another less important but nonetheless noticeable difference is that passing a reference to a very large array is significantly faster than the simple aliasing provided by Perl (which is itself significantly faster than the passing-by-value option for the obvious reason that passing-by-value is just aliasing followed by data copy into local variables). On a benchmark that I once made, simple aliasing was 18 to 20 times faster than passing-by-value, and actual reference passing was almost 500 times faster than aliasing. Having said that, aliasing is really pretty fast, you would need to pass really huge amount of data to really worry about the difference.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-25 20:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found