On Wed, 20 Nov 2002 14:43:07 +0100, Abigail wrote:

>> >sub commify
>> >{
>> >    my ( $max, $sep, $end ) = ( shift, shift, shift );
>>      ...
>> >}
>> 
>> Wow! Hold it! Am I the only one who finds this absurd? More than one
>> shift on the same array in one single expressing, sounds like bad style
>> to me. Comments?
>
>
>Why is that bad style? Many times when people say it's bad style,
>it's just a case of "beauty is in the eye of the beholder". 

>However, sometimes a style is bad because it's error-prone, 
>confusing, similar to common idiom but doing something else,
>or inefficient. But I don't think any of them applies to this
>particular example.

>Bart, can you explain why this is bad style? Or is it just your
>personal preference?

It is because it comes into the realm of undefined behaviour. What it
does is execution-order dependent, in the least. It makes the programmer
think too hard on what it does. It is as bad as

        my($x, $y, $z) = ($i++, $i++, $i++);

You may be in luck, and it may do precisely what you want, but by the
time you made sure it does, you've already wasted far too much time on
it. Here is an example that mosty likely *not* do what you want:

        $i = 20;
        my($x, $y, $z) = ($i++, $i, $i++);

As to what the next does,

        $i = 20;
        my($x, $y, $z) = ($i++, +$i, $i++);

your guess is a good as any.

As for an alternative, my first choice would be the simple array
assignment:

        my ( $max, $sep, $end ) = @_;

If you *need*, to modify the argument list, I'd first think of the three
separate shift statements, but more likely, as Bernie Cosell suggested,
I'd use the splice.

        my ( $max, $sep, $end ) = splice  @_, 0, 3;

If this stings your eyes, may I suggest adding a optional second
argument to shift(), a number of items to shift, as

        my ( $max, $sep, $end ) = shift @_, 3;

In scalar context, shift() when used with it could return the last item
of the shifted out list.

-- 
        Bart.

Reply via email to