[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Testing list?



John Porter wrote:
> Peter Haworth wrote:
> > 
> > What's actually happening here is that the LHS is being evaluated in
> > scalar context, which means that it's the scalar value of @c which is
> > being tested for truth - $a and $b are evaluated, then thrown away.
> 
> Right.  I don't see this as a problem. 

It just makes me uncomfortable. You're basing the truth on part of the target.
This is only slightly horrible for arrays on the end:

  ($a,$b,@c)||=@foo;
Here, whether the LHS is assigned to depends entirely on whether @c contains
any elements. Slightly odd, but not too bad.

  ($a,$b,$c)||=@foo;
Here, the assignment depends on $c being false. That's extremely peculiar to
me.

  (@a,@b)||=@foo;
Here, if @b contains any elements, nothing happens, but if it doesn't, @a is
filled from @foo, and @b is emptied (though it's already empty). This is
equivalent to:
  @a=@foo unless @b;
Yuck!

  @a||=@foo;
This is the only case which makes any sense to me.

> The problem with the proposal, as pointed out by 
> other commentators, is that the LHS would need to be
> evaluated in both scalar and list contexts -- the
> former for the test, the latter for the assignment -- 
> and perl is currently not capable of this.

As far as I remember, p5p didn't like it for that reason.


> I'm not proposing any changes in semantics (other than
> to make the construct legal).  As I said:
> 
>          ( $a, $b, @c ) ||= ( 1, 2, 3, 4, 5 );
>     would be the same as
>          ( $a, $b, @c ) or
>          ( $a, $b, @c ) = ( 1, 2, 3, 4, 5 );
>          
> I believe this could even be done as a compiler macro.
> That would obviate the need to add new interpreter
> semantics.

Except that we don't have compiler macros yet. I haven't seen anyone volunteer
to implement them either.

> Programmers would just have to be aware that
> the LHS gets evaluated twice.

It doesn't get evaluated twice, it's more or less the same as scalar ||= . The
LHS is evaluated, and assignment is done if it comes out false. The problem
with this in array context is that the LHS is evaluated in scalar context, and
assignment is done in array context if it's false.

-- 
	Peter Haworth	pmh@edison.ioppublishing.com
fix /n.,v./: What one does when a problem has been reported
	     too many times to be ignored.
	-- Jargon File v4.0.0


==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe