Re: AW: Function parameter passing (was: Re: limit the list)
--- Pense, Joachim [EMAIL PROTECTED] wrote: Von: Moran, Matthew [mailto:[EMAIL PROTECTED]] Gesendet am: Mittwoch, 20. November 2002 14:41 Joachim suggested: sub commify { my $max = shift; my $sep = shift; my $end = shift; ... } better or even worse in your view? Clearer, but just as bad IMHO. I've always done it as sub subroutine( my ($list, of, $variables) = @_; #rest of code here } It's how it's always shown in the Cookbook what have you. Not *always*, I've seen my version explicitly suggested somewhere. The advantage in my eyes is that you consume the input parameters so they are gone when you read them. It is also easier to check in the end if there are any left so too many were supplied. You mean you saw it in the holy book itself: Programming Perl (aka The Camel). Looking at page 374 we find: sub TIEARRAY { my $class = shift; my $bound = shift; confess usage: tie(\@array, 'BoundedArray', max_subscript) if @_ || $bound =~ /\D/; return bless { BOUND = $bound, DATA = [] }, $class; } To bring this back inline with the original discussion, I was thinking the problem could be solved with a tied array. Starting at 372 it shouldn't take long to implement the required code - left as an exercise for the reader. Jonathan Paton = s''! v+v+v+v+ J r e Ph+h+h+h+ !s`\x21`~`g,s`^ . | ~.*``mg,$v=q. P ! v-v-v-v- u l r e r h-h-h- !12.,@.=m`.`g;do{$.=$2.$1,$.=~s`h E ! v+v+v+ s k e h+h+ !`2`x,$.=~s`v`31`,print$.[$v+=$.] R ! v-v- t H a c h h- !}while/([hv])([+-])/g;print\xA L ! A n o t !';$..=$1while/([^!]*)$/mg;eval$. __ Do You Yahoo!? Everything you'll ever need on one web page from News and Sport to Email and Music Charts http://uk.my.yahoo.com
Re: AW: Function parameter passing (was: Re: limit the list)
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
Re: AW: Function parameter passing (was: Re: limit the list)
-- Pense, Joachim [EMAIL PROTECTED] 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; Until someone does my $sep = shift; my $max = shift; my $end = shift; or my $sep = shift; my $max = shift; to it. Especially the former has caused people who work for me pain, which is why I adopted the one-line method. That or splice. -- Steven Lembark 2930 W. Palmer Workhorse Computing Chicago, IL 60647 +1 800 762 1582
Re: AW: Function parameter passing (was: Re: limit the list)
On Wed, 20 Nov 2002 13:34:40 - Pense, Joachim [EMAIL PROTECTED] 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; ... } I use this form too. it is more explicit and gives nice way to comment: sub commify { my $max = shift; # this is arg 1 blah my $sep = shift; # arg two blah my $end = shift; # arg III, actually takes hash reference to useless data :) ... } which is better than my ( $max, # ala $sep, # bala $end ) # nica = @_; imo. it is matter of taste of cource... my ( ... ) = @_; has the only advantage to be ~20% faster for large number of function call iterations. finally: sub nonsensessez { my $s = $_[0]; my $a = $_[1]; my $k = $_[2]; my $j = $_[3]; my $l = $_[4]; 1; } combines the best from both forms above ( i.e. cna be commented, clean and approx. as fast as `my ( ... ) = @_' thing. P! Vladi. better or even worse in your view? Joachim -- Vladi Belperchinov-Shabanski [EMAIL PROTECTED] [EMAIL PROTECTED] Personal home page at http://www.biscom.net/~cade DataMax Ltd. http://www.datamax.bg Too many hopes and dreams won't see the light... msg02736/pgp0.pgp Description: PGP signature
Re: AW: Function parameter passing (was: Re: limit the list)
-- Vladi Belperchinov-Shabanski [EMAIL PROTECTED] On Wed, 20 Nov 2002 13:34:40 - Pense, Joachim [EMAIL PROTECTED] 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; ... } I use this form too. it is more explicit and gives nice way to comment: sub commify { my $max = shift; # this is arg 1 blah my $sep = shift; # arg two blah my $end = shift; # arg III, actually takes hash reference to useless data :) ... } which is better than my ( $max, # ala $sep, # bala $end ) # nica = @_; imo. it is matter of taste of cource... my ( ... ) = @_; has the only advantage to be ~20% faster for large number of function call iterations. finally: sub nonsensessez { my $s = $_[0]; my $a = $_[1]; my $k = $_[2]; my $j = $_[3]; my $l = $_[4]; 1; } combines the best from both forms above ( i.e. cna be commented, clean and approx. as fast as `my ( ... ) = @_' thing. Only to the extent that you are not processing the remainder of @_ after taking the fixed parameters -- that or you have to remember to use the proper offset in a foreach or array slice to access the argument array in for/map/grep. -- Steven Lembark 2930 W. Palmer Workhorse Computing Chicago, IL 60647 +1 800 762 1582