Monday, 14 January 2013

Perl Notes -Subroutines


Subroutines

sub() foo {
my($x, $y) = @_; ## parameters
my($z); ## local var
return($x + $y);
}
foo(6, 7) ==> 13
@_
@_ is an array of the passed scalar values. Use '=' to make a parallel assignment into
local variables (values must be scalars).
Flattening
Cannot pass an array as one of the args – it all gets "flattened" into @_
my
my() declares something local
Use strict – $::global
Refer to outer global vars as $::global – needed if the "use strict;" or "use strict 'vars';"
option is on. Use strict is a good idea for a program more than a page or two long.

No comments:

Post a Comment