Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: Subroutine's behavior

by tford (Beadle)
on Nov 23, 2009 at 01:38 UTC ( #808707=note: print w/ replies, xml ) Need Help??

in reply to Subroutine's behavior

The reply above mine pretty much says it all. However, you might be wondering what the shift is for.

The shift function takes an array as input, and "shifts" off the first element of the array. Then it returns that element, leaving the array one element shorter.

For example,

my @array = (2,3,4); my $value = shift(@array); # now the array contains (3,4) print "value is $value";

Now, if you don't specify an array to use as input, the shift function works on the "default array" called @_.

The last important thing to know is that anytime you call a Perl subroutine, you only ever call it with a flat array of arguments. Within the scope of the subroutine, those arguments are automatically assigned to the @_ array.

Thus shift shifts the first argument off the argument array, and assigns it's value to the variable $word, which is local to the subroutine.

Hope this helps!


Comment on Re: Subroutine's behavior
Select or Download Code

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://808707]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2016-05-31 13:34 GMT
Find Nodes?
    Voting Booth?