Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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!

~tford


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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2014-07-13 10:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (249 votes), past polls