On Wed, 20 Nov 2002, Pense, Joachim wrote:

> Bart Lateur [mailto:[EMAIL PROTECTED]] wrote:
> (Mittwoch, 20. November 2002 11:43)
> 
> >On Wed, 20 Nov 2002 04:10:02 -0600, Steven Lembark 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?
> 
> In one of my programs, this would be
> 
> sub commify {
>     my $max = shift;
>     my $sep = shift;
>     my $end = shift;
> 
>     ...
> }
> 
> better or even worse in your view?

Better, but only if it makes sense to conditionally take values off the 
calling stack, like this contrived example:

sub foo {
        my $type = shift;
        my $arg1 = shift;

        my $arg2 = 0;

        if ($type == 'APIv2') {
                $arg2 = shift;
        }

        return $arg1 + $arg2;
}

A variation of this would be if, depending on the type, you might want 
different names for a given element on the calling stack:

sub foo {
        my $type = shift;
        my $arg1 = shift;

        if ($type == 'APIv2') {
                my $adder = shift;
                return $arg1 + $adder
        } elsif ($type == 'APIv3') {
                my $subtractor = shift;
                return $arg1 - $subtractor;
        }

        return $arg1;
}


You might also do something like this to illustrate that depending on the
value of the first argument, we may not even care about any of the rest.

sub foo {
        my $type = shift;

        if ($type == 'APIv3') {
                carp "APIv3 unsupported\n";
                return;
        }

        # perhaps do computationally intensive or transactional stuff here
        # ..

        my $arg1 = shift;
        my $arg2 = 0;

        if ($type == 'APIv2') {
                $arg2 = shift;
        }

        return $arg1 + $arg2;
}

Granted, there's no performance benefit, but since certain variables
aren't introduced until after the conditional return case, it definitively
shows that those values aren't needed unless we pass the condition.

I think that using 'shift' as a little faucet that gives you stuff as you
need it is rather elegant.

--Jeremy

-- 

Jeremy Impson
Sr. Associate Network Engineer
Investigative Research & Marketing
Lockheed Martin Systems Integration
email: [EMAIL PROTECTED]
phone: 607-751-5618
fax:   607-751-6025


Reply via email to