This reference stuff carried over, of course, from C. Both are useful in certain situations, you simply must recognize them. Really, the speed issue should be an afterthought. When you pass by reference, you are passing almost the original object to the function. You can use this variable as if it had been called from the encapsulating function. When you return, the variable will remain what you set it in the calling function since any alteration in the function will have effect on the original variable. When you pass by reference, you are actually passing the variable's pointer address in memory.
Now, when you pass by value, you are passing the actual variable's value when you called the function from the caller. This value is taken and a personal copy for that function and that function only is created. Any changes made to that copy do not affect any other variable in the system.
Now, to your issue. In C, some variables are actually smaller than passing a pointer (reference), so the passing by value was actually preferable over passing by reference! But in Perl, there are no data types- even strings are held in the same variable. A typical string (keeping it simple) will easily eat up more than 16 or 32 bits (a typical pointer size). While internally, the pass is made with a pointer, perl will create a dynamic memory copy at some point before it continues in your code. If you do not need another copy to work on, pass by reference. In C, there is no penalty for pointers (references). In Perl, the penalty can become substantial- especially if you are derefencing in a loop- especially in a main or very important loop! In all cases, you should
- consider whether you really need a function. Questions: Will i reuse this code again? Will I not be using it all too often (perhaps for a simple conversion only)? Do I need recursion?
- consider which variables that you need to pass. Questions: Is this variable relevant in the function context?
- choose pass ref or pass val. Questions: Will this variable remain constant throughout my function?(pass by val unnecessary) Do I need to take this variable and change it even in the encapsulating function? (reference required) Do I need my own copy of the variable for my own use without affecting other variables? (pass by val necessary unless you ref and then copy manually)
Lastly, comes the speed check. As others have mentioned, Perl dereferencing takes time, so a
Benchmark would be a good way to fly. On the other hand, sometimes it's obvious or irrelevant. Is the function only called once or twice in the program? Then the testing really won't reveal any mind-boggling speed increase. If the function is mission-critical and you really need that speed increase, go for it. Anyway, if you're using Perl, you're really not writing any OpenGL routines, so you should be all set with whatever you choose. If the shoe fits, wear it. One help may be to make sure that the user always has something to see. That means piping data straight to the user as it comes up (or in buffers thereof). That way, even if your program isn't fast, it'll seem fast!
Q: Could a variable assignment bypass the perl dereference mechanism, as in:
my $var=$bar->foo(); #deref here
&var; #yet another deref?
? thanx.