develooper Front page | perl.perl5.porters | Postings from March 2001

Re: Distributive -> and indirect slices

Thread Previous
David M. Lloyd
March 26, 2001 06:19
Re: Distributive -> and indirect slices
Message ID:
On Mon, 26 Mar 2001 wrote:

> > > > My thinking was that not too many people use the current
> > > > behaviour... maybe for Obfuscated Perl contests, but the operator could be
> > > > so much more powerful than it is right now.
> > > 
> > > Eh, don't forget it also provides scalar context to 'method' in:
> > > 
> > >     $obj -> method -> other_method;
> > > 
> > > I don't think methods returning objects in scalar context and lists
> > > in list context are obfuscated. That's what's wantarray is about, isn't?
> > 
> > That behaviour wouldn't change, since the leftmost expression is a scalar.
> Wait, that doesn't make sense.
>     @a = $obj -> method;
> calls method in list context because of the assignment. Now you are
> suggesting that it should call method in scalar context because of '$obj'?

No, I'm suggesting that the behavior shouldn't change becuase of '$obj'.  
That is, if the leftmost expression is a scalar, the expression will take
the context that it would currently have under our Perl 5 rules.

However if the leftmost expression is a list, the whole expression should
be in list context because all return items will be flattened into a list,
and it just doesn't make sense to me to have these functions be called in
scalar context.  I mean, there'd be no way to specify what you want.  Take
these examples:

$a = $obj->method;  # Clearly scalar context.
@a = $obj->method;  # Clearly list context.
$obj->method;       # Clearly void context.

$a = $obj->method1->method2;  # Both methods in scalar context.
@a = $obj->method1->method2;  # method1 in scalar context, method2 in list context.
$obj->method1->method2;       # method1 in scalar context, method2 in void context.

@a = ($obj1, $obj2)->method;    # Clearly list context.  Results of method
                                # calls flattened into a list.

$a = ($obj1, $obj2)->method;    # Unclear.  Calling in scalar context
                                # gives no benefit, since you will
                                # still have 2 return values. Maybe call
                                # methods in list context anyways and
                                # give the length of the return list?
                                # Or else return rightmost value.

($obj1, $obj2)->method;  # Same deal.  Might as well call in list context.

@a = ($obj1, $obj2)->method1->method2;
# More complicated.  Call method1 in list context on $obj1 and $obj2.
# Call method2 in list context on each of method1's return values.
# Results flattened into a list, assigned to @a.

$a = ($obj1, $obj2)->method1->method2;
# Unclear context, might as well be list.  Call like above, return list
# length or rightmost value, depending on which would be less useless.

($obj1, $obj2)->method1->method2;
# Call like above, return value chucked.

I hope this clarifies.

NOTE: Maybe this discussion should continue on Perl6-language?

- D


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