Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^7: nodelocked vs floating

by kcott (Archbishop)
on Nov 25, 2018 at 07:16 UTC ( [id://1226281]=note: print w/replies, xml ) Need Help??


in reply to Re^6: nodelocked vs floating
in thread nodelocked vs floating

The example I showed with $href->%* was highly simplistic. It was intended to show that, with 5.28, I could use the new syntax and didn't need to include a 'use feature' statement.

When dealing with simple references, I would typically use @$aref, %$href, and so on, rather than $aref->@*, $href->%*, etc.

When working with complex data structures, that simple reference becomes a series of keys and indexes read from left-to-right. Adding a postfix dereference continues that left-to-right progression: you don't need to scan back to the beginning of the expression to determine what dereferencing is being used. Consider these two:

$href->{$key1}[$i]{$key2}->%*; %{$href->{$key1}[$i]{$key2}};

Of course, they're only short, example variable names. In production code, I'd be using meaningful variable names:

$meaningful_href->{$meaningful_outer_key}[$meaningful_index]{$meaningf +ul_inner_key}->%*; %{$meaningful_href->{$meaningful_outer_key}[$meaningful_index]{$meanin +gful_inner_key}};

Here, the benefit of not having to scan back to the beginning of the expression becomes more obvious.

Except for the simple references, like %$href, there's only one additional character when using the postfix syntax: I wouldn't consider that to be a reason to use, or not use, it.

When whatever is being dereferenced is more complicated than a simple reference, I would recommend adding the braces (e.g. prefer @{$complex_ref} over @$complex_ref). Even when @$complex_ref seems completely transparent to the author, it might easily be less obvious to the next maintainer.

Using postfix dereferencing, in the contexts I've described above, is my personal preference for the reasons given. I would recommend its use to others for the same reasons. I certainly wouldn't suggest anyone must use it.

In "Re^3: nodelocked vs floating", I did point out that it took me "a little while to get used to": if you're considering giving it a try, you might also want to give it a little while also.

Finally, some practical notes on usage:

  • I've been writing Perl code for $work for about 25 years and, in all that time, I've never been in a position to write code requiring a Perl version greater than 5.16: so I've never used postfix dereferencing in any actual production code. Consider your clients' resources before adding a use 5.018; (or later version) to your code.
  • For CPAN code, unless there was some other overriding reason to require 5.18 or later — for example, there may be a need to support a particular Unicode version — I wouldn't recommend postfix dereferencing here.
  • I have used postfix dereferencing extensively in my own personal code. I'm very happy with it.

— Ken

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-20 01:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found