in reply to Re: Pass By Reference Inner Workings - Magic scalar operator
in thread Pass By Reference Inner Workings - Magic scalar operator

Am I correct that by saying something like
sub mysub { my %x = %{+shift}; }
I'll actually be copying all of the hash entries into a local variable... So if the hash passed into the subroutine is very large, it could be a performance hit? The paradigm I usually use is to shift into a scalar and then use dereference operator to access elements. This may be why... unless you have really good reasons to do it, should one stay away from the paradigm in the code segment above???

Replies are listed 'Best First'.
Re^3: Pass By Reference Inner Workings - Magic scalar operator
by davido (Cardinal) on May 01, 2005 at 08:45 UTC

    First thing's first; diotalevi is right, my %x = %{shift()}; is by far the preferred method. I pulled out the unary + as a prefix that served a similar purpose to your use of scalar in this situation. But it's ultimately a bad example, and shift() is the proper way to do things. Treat a function like a function, (ie, use parens) rather than using the unary plus +.

    Now to answer your question: Yes, my %x = %{shift()}; makes a copy of the contents of the hash to a lexical variable. This does cost performance and memory. But there are times when you need a copy, and other times where it's fine to just work with the reference to the original hash, held in $_[0].


    Dave