Re: Shift Question



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
.



Relevant Pages

  • Re: Shift Question
    ... > shift in subrutines and how does shift work. ... > shift works on lists, it removes the first element of the list ... perldoc -q "What is the difference between a list and an array" ... The term 'lvalue' has a specific meaning in Perl which usually means ...
    (perl.beginners)
  • Re: Return subset to drop down box
    ... I have a workbook to track production. ... input which shift they are inputting data for via drop ... employees who are on second shift to another drop down box on the ...
    (microsoft.public.excel)
  • Shift Question
    ... shift in subrutines and how does shift work. ... shift works on lists, it removes the first element of the list (the 0 ... perldoc -f unshift ...
    (perl.beginners)
  • Re: difference between "@_" and "shift"
    ... shift is a function for pulling off the first element from a list. ... You can push to and pop from the end ... of lists to treat them as stacks; you can shift from and unshift to the ...
    (perl.beginners)
  • Re: Automatic Capitalization
    ... >> You will have to train your users to use their Shift keys. ... >>> We have some users who are creating numbered lists, and are doing a bit of ... >>> Capitalize the first letter of sentences is turned on in AutoCorrect ... To paraphrase what Wilma mentioned in the thread "Re: numeric list question" ...
    (microsoft.public.word.newusers)