When the poll was published I truthfully answered with "Never". However, since then I find myself checking whenever I write @{$stuff} whether it would be more readable as a postfix dereference. Eily already brought up one border case in Re^2: I use postfix dereferencing ..., but I'd agree with him that in this case an intermediate variable would be preferable.
Today I stumbled over another use case where I am now tempted to use postfix dereferencing: I'm pulling several attributes out of an object, some of which are array references... and I need copies of the arrays. In that case, scalars and arrays "line up" better with postfix dereferencing. SSCCE:
use 5.020;
use feature 'postderef';
package Object {
use Moose;
has string => (is => 'ro', isa => 'Str');
has aref => (is => 'ro', isa => 'ArrayRef');
};
my $object = Object->new(string => 'Hello World',
aref => [1,2,3]
);
# Traditional dereferencing
my $string = $object->string;
my @list1 = @{$object->aref};
# Intermediate variable
my $string = $object->string;
my $aref = $object->aref;
my @list2 = @$aref;
# Postfix dereferencing
my $string = $object->string;
my @list3 = $object->aref->@*;
What needs work in my opinion is the documentation in perlref. It claims that (emphasis mine) Postfix dereference should work in all circumstances where block (circumfix) dereference worked, and should be entirely equivalent. "Should work"? Except when it doesn't? I'd prefer not to rely on syntax which should work. A bit later it says: Note especially that $cref->&* is not equivalent to $cref->(), and can serve different purposes. I admit that I'm too lazy to work out the difference and have decided not to use the ->&* construct instead.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|