Shift and unshift do the same thing to the left end of an array that pop and push do to the right end. This is a typical metaphor for creating a local copy of something passed to a function. You may also see this: which makes a local array from an array reference passed as an argument.
David -- Remove "microsoft. Sorry, but most of my junkmail comes from addresses harvested from the newsgroups. I'm also considerate enough to read replies on news. Well, yes. Well, basically, that is syntactic sugar. Wed, 18 Jun GMT h.. Hi, at method call.
And the largest number yet seen is the only one yet seen: 3 , the first parameter. And that was the last time through the loop, so the loop is done. But what happens if there are none? At first, it may seem too esoteric to worry about. But maybe someone wrote a line like this one:. And the array numbers might sometimes be an empty list; perhaps it was read in from a file that turned out to be empty, for example.
Of course, whoever called this subroutine should be aware that the return value may be undef —or they could simply ensure that the parameter list is never empty.
For example, they can be used in the block of an if , while , or foreach :. As experienced programmers have learned often the hard way , limiting the scope of a variable to a page of code, or even to a few lines of code, really speeds along the development and testing cycle. So, when reading code like this, you can always tell the context of the assignment by seeing what the context would be without the word my.
Remember that without the parentheses, my only declares a single lexical variable: [ ]. Of course, you can use my to create new, private arrays as well: [ ]. Any new variable will start out empty— undef for scalars, or the empty list for arrays. In Chapter 3 , you saw that you could define your own control variable with the foreach structure.
You can make that a lexical variable, too:. This is important in the next section, where you start using a feature that makes you declare all your variables.
Perl tends to be a pretty permissive language. A pragma is a hint to a compiler, telling it something about the code. Why would this be important? Now, you keep typing for a while. After that line has scrolled off the top of the screen, you type this line to increment the variable:. Since Perl sees a new variable name the underscore is significant in a variable name , it creates a new variable and increments that one. Starting with Perl 5. Now, among other restrictions, [ ] Perl will insist that you declare every new variable, usually done with my : [ ].
Most people recommend that programs that are longer than a screenful of text generally need use strict. And we agree. What if you want to stop your subroutine right away? The return operator immediately returns a value from a subroutine:.
This is the most common use of the keyword return in Perl—to return a value immediately, without executing the rest of the subroutine. But what if you never found that element? It would be more Perlish, perhaps, to return undef in that case, but this programmer used —1.
In that case, you can even omit the parentheses around the argument list:. The compiler has to see the definition before the invocation in order to use the subroutine call as if it were a built-in.
The catch is this: if the subroutine has the same name as a Perl built-in, you must use the ampersand to call your version. That means that you will use it for your first hundred programs or so.
If you call your subroutine in a list context, [ ] it can return a list of values. Suppose you want to get a range of numbers as from the range operator,.. The least you can return is nothing at all. A return with no arguments will return undef in a scalar context or an empty list in a list context. The confusion here is that it appears shift is not being passed an array as an argument. The shift function removes the first element from an array, and returns it. The array is shortened by one element.
Your code is identical to:. In layman's terms, from a very highlevel view, shift is taking the first element of an array the leftmost part , while the opposite is pop which is taking the last element of array the rightmost part. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more.
What does shift do in Perl? Ask Question. Asked 12 years, 11 months ago. Active 1 year, 10 months ago. Viewed 47k times. What could the following line possibly mean? Improve this question.
Peter Mortensen 29k 21 21 gold badges 97 97 silver badges bronze badges. Perl has built-in docs for every standard function. They're also online.
0コメント