Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

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!


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 about the Monastery: (8)
As of 2016-10-26 21:06 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (348 votes). Check out past polls.