develooper Front page | perl.perl5.porters | Postings from July 2013

Re: postfix dereference syntax

Thread Previous | Thread Next
Nicholas Clark
July 2, 2013 11:12
Re: postfix dereference syntax
Message ID:
On Wed, Jun 26, 2013 at 09:59:02PM -0400, Ricardo Signes wrote:
> We can add postfix syntax for dereferencing to work in *all* places that we
> might dereference, creating a construct that is clearly syntactically an array
> (or hash, etc.) when needed.
>   push $x->{foo}->[0]->m->@*, $y;
>   push @y, $x->{foo}->[0]->m->@*;
>   subroutine( $x->{foo}->[0]->m->@* )
>   print $delivery->{email}->body_string_ref->$*;

I would be pleased to have a syntax that can be used anywhere that a
circumfix can be used. It avoids learning/remembering a special case about
where it works, and where it doesn't.

I'm going to assume that the wording defining prototypes would need to be
carefully re-written to find a clear terse way to say that

   sub foo(\@@);
   foo $x->{foo}->[0]->m->@*, $y;

is considered to have an @ at the front of that first argument, so that
the above code is legal.

> If we want the two forms to be really of equivalent value, we'll also need to
> be concerned with:
>   print "Things: $aref_of_things->@*"
> ...which gets into less clearly-introduceable behavior.

This is "less clear" because right now that parses as

print 'Things: ' . $aref_of_things . '->@*'

not as a syntax error? ie we're changing the meaning of something valid.

> Finally, do we need to enable postfix slices?  I think that if we can, we
> should.  I think the syntax is free.
>   say for $href->{aref}->@[ 0, 2, 4 ];
>   say for $aref->[ $h_idx ]->@{ qw(foo bar baz) };
> I think we have a few options for the specific tokens to put after that last
> arrow.  I think $* and @* and so on work well.  Replacing the splat with a
> colon has been suggested as well.  The point here is to provide the feature.
> It's not about reducing total punctuation.  This is Perl, and we must embrace
> our punctuation!  It's about simplifying the reading of code by allowing it to
> be read and written linearly.

Are there parsing ambiguities with a colon? Particularly, if a colon is
expected because the opening '?' of a ternary has been seen, does this
confuse things? Perl 6 initially doubled the symbols of '?' and ':' to enable
':' to be used in a lot more places. (and then changed '::' to '!!' I think
to further reduce ambiguities)

I'm not sure that ':' is equal to '*'. Whilst both are currently valid for
a scalar splat syntax:

$ perl -le '$a = bless []; *{""} = sub {warn "Hi!"}; print $a->$*'
$* is no longer supported at -e line 1.
Hi! at -e line 1.
$ perl -le '$a = bless []; *{" \n-"} = sub {warn "Hi!"}; print $a->$:'
Hi! at -e line 1.

Existing code that uses $* warns. $: doesn't.

Nicholas Clark

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About