Perl Monk, Perl Meditation | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
G'day chenhonkhonk, Welcome to the Monastery. Those first two expressions you wrote are not the same:
You can apply postfix dereferencing to the second one, like so:
Note that's "->$*", not "->%*". In your last expression (with keys), your problem appears to be that you're attempting to dereference twice: once with a "%" at the start, and again with a "->%*" at the end.
One of these two is probably what you are after:
As a general rule, when dereferencing anything more complex than a simple scalar (e.g. %$href, @$aref, etc.), and not using postfix dereference syntax, I'd recommend wrapping the reference in braces and adding the appropriate sigil in front of that (e.g. %{ ... }, @{ ... }, etc.): this makes both the code, and your intent, very clear. Some might suggest you do this always; my personal view is that it's unnecessary in the simplest cases. When I first saw postfix deference syntax as an experimental feature in 5.20 ("perl5200delta: Experimental Postfix Dereferencing"), I thought it looked a bit weird and, as I generally avoid experimental features, didn't give it much further attention. However, I started using it in 5.24 when the "experimental" flag was lifted ("perl5240delta: Postfix dereferencing is no longer experimental") and now I much prefer it. I think it makes the code easier to read: there's a straightforward left-to-right progression; as opposed to having to go backwards to find the sigil, then downwards into nested braces to find what that sigil refers to. For perlref documentation, see "Postfix Dereference Syntax" and "Postfix Reference Slicing". — Ken In reply to Re: 5.26 sigil reference syntax in subfunction
by kcott
|
|