Mulander wrote: > If I understood you question properly you want to know why people use > shift in subrutines and how does shift work. > > I will try to make it short: > shift works on lists, it removes the first element of the list ( the 0 > indexed element ) and returns it as a lvalue ( if there are no more > elements in a list it returns undef ). > Here is an example: > > my @list = qw(a b c d); > print shift @list,"\n"; > print "my list is now @list\n"; > print shift @list,"\n"; > print "my list is now @list\n"; > print shift @list,"\n"; > print "my list is now @list\n"; > print shift @list,"\n", > print "my list is now @list\n"; > > this should print something like this ( althoug I did not have the > time to test it ) > a > my list is now b c d > b > my list is now c d > c > and so on... > If you use shift without giving it the list name to work on it will > refer to @_ or @ARGV ( it is decided upon the file scope ). So when > you define a sub like this: > > sub somesub { > my $arg1 = shift; > } > > You did something simmilar to my $arg1 = $_[0]; but more elegant ( in > my opinion ) and you removed the first element from the arguments list > ( witch is quite usefull ). > > Hope this will clear some things up, you can check also: > perldoc -f shift > perldoc -f unshift > perldoc -f pop > perldoc -f push >
Additionally a very common idiom and where you may be seeing this so much is within OOP style programs/modules. In the case of object oriented syntax Perl automagically tacks on the class name or instance object as the first argument to the subroutine (method). So you will very commonly see, sub class_method { my $class = shift; my (%other_args) = @_; } or sub instance_method { my $self = shift; my (@other_args) = @_; } These would be called like, Class::Object->class_method(key1 => 'val1'); or my $object = new Class::Object; $object->instance_method('arg1','arg2'); I often use this syntax for writing non-OOP libraries just because it has become so common otherwise. In which case having the name of the class really doesn't matter much. Finally, it is common to use 'shift' with subroutines that take a very common x # of arguments, and then take a "hash" like list. HTH, http://danconia.org -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>