Function Footnotes

Now that you know about scope, there are some things that can be done effectively only with scope. One is recursive subroutines, and the other is the use strict Perl statement that enables a stricter Perl, possibly preventing you from making mistakes.

Declaring Variables local

Perl version 4 didn't have private variables. Instead, Perl 4 had variables that were “almost private.” This concept of almost-private variables is still around in Perl 5. You declare these variables by using the local operator, as in this example:

sub myfunc {
    local($foo)= 56;
    # rest of function...
}

In the preceding snippet, $foo is declared to be local to the myfunc() subroutine. A variable that has been declared with local acts almost identically ...

Get SAMS Teach Yourself Perl in 24 Hours THIRD EDITION now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.