in reply to Re: Sending an anonymous hash to a sub
in thread Sending an anonymous hash to a sub
So I can do this:
and that " %person = %{+shift}; " thing is just the above, compressed into a one-liner -- is that right? Something about the plus sign just looks so weird to me...sub describe { $ref = shift(); my %person = %{$ref}; print "$person{name} is a $person{job}" }
($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss') =~y~b-v~a-z~s; print
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Sending an anonymous hash to a sub
by BrowserUk (Patriarch) on Sep 03, 2003 at 06:22 UTC | |
Be aware that doing
You are not "passing an anonymous hash" you are
NOTE: This is far from an accurate picture, but the main inaccuracies are in the steps that I haven't expanded! The point is that by constructing an anonymous hash from a list, passing that hash, by reference to a sub, then flattening that hash back to a list and then constructing a new hash from that list, you are doing a huge amount of work that is completely unnecessary and wasteful. It is true that perl does this in the "twinkling of an eye", but by avoiding it, it could achieve the same ends in a "twinkle of a twinkling". If your going to use named parameters to subs, either pass a list in and construct the hash (once) at the destination.
Or, construct the anonymous hash in the calling program, pass a reference and use that reference to access the hash you constructed.
The way you are doing it here is just sooo wasteful. For two element hashes with short keys and one depth of call, the overhead is probably not dramatic in realtime terms. But if you use this practice for larger hashes with longer keys and going a several levels deep, it really begins to mount up. By way of demonstration, these results from the benchmark below show the difference in using 4 different methods of passing two strings to a sub and using those parameters to contruct a string for printing.
For this set of results, the sub only constructed the string and didn't actually print it. Print is a relatively expensive operation. One view is that if the sub you are calling is going to do anything substancial (like calling print), the overhead of the parameter passing and access tends to pail in to insignificance. After all those numbers above show that describe1() will process 5,186 pairs of parameters in a second or one pair every 193 microseconds. Okay, but describe4() does 24,080 in a second or 1 every 41 microseconds. but to be fair, here are the results from the same benchmark, this time actually calling print.
As you can see, that slows thing down considerably. describe4() is now only 2 1/2 times quicker rather than 3 1/2 times quicker than describe1(). And, if the sub was doing anything more substantial, then this difference is further depleted, but it is worth thinkng about when you code your subs. The benchmark code Read more... (2 kB)
Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller If I understand your problem, I can solve it! Of course, the same can be said for you. | [reply] [d/l] [select] |
|
Re: Re: Re: Sending an anonymous hash to a sub
by bobn (Chaplain) on Sep 03, 2003 at 04:13 UTC | |
the + is used (I think) to dis-ambiguate the notation - other wise %{shift} could actually be interpeted as %shift - at least in some contexts. --Bob Niederman, http://bob-n.com All code given here is UNTESTED unless otherwise stated. | [reply] |