develooper Front page | perl.perl5.porters | Postings from January 2014

Re: Range idea

Thread Previous | Thread Next
From:
Zefram
Date:
January 23, 2014 13:03
Subject:
Re: Range idea
Message ID:
20140123130331.GF21945@fysh.org
H.Merijn Brand wrote:
>Why cannot we use open range operators in this case?

They wouldn't really be range operators.  @foo[3..5] is the composition
of a range operator with the @foo[LIST] operator.  Crucial to this
is that the range expression 3..5 is meaningful on its own: in list
context it generates the list (3,4,5).  But your open range couldn't
work that way.  In @foo[3..], the 3.. doesn't mean anything specific on
its own.  That doesn't mean we can't do it at all, but it does mean that
@foo[SCALAR..] is a specific operator unto itself, and the open range
can't be used anywhere else even though it looks like it could be used
in a different context.  It's not a nice design.

>$ perl -wE'say for [qw( a b c d )]->[2 ..]'

This is actually not a correct analogy.  $REF->[EXPR] corresponds to
${$REF}[EXPR], not to @{$REF}[EXPR].  It always puts the index expression
in scalar context, so if a range operator is used there it acts as the
flip-flop operator, not a range generator.

-zefram

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About