next up previous contents
Next: Using a List as Up: Functions Previous: Passing Parameters by Reference

Scope of Variables

Scope refers to the visibility of variables. In other words, which parts of your program can see or use it. Normally, every variable has a global scope. Once defined, every part of your program can access a variable.

It is very useful to be able to limit a variable's scope to a single function. In other words, the variable wil have a limited scope. This way, changes inside the function can't affect the main program in unexpected ways, func7.pl

firstSub("AAAAA", "BBBBB");



sub firstSub{

    local ($firstVar) = $_[0];

    my($secondVar)    = $_[1];



    print("firstSub: firstVar  = $firstVar\n");

    print("firstSub: secondVar = $secondVar\n\n");



    secondSub();



    print("firstSub: firstVar  = $firstVar\n");

    print("firstSub: secondVar = $secondVar\n\n");

}



sub secondSub{

    print("secondSub: firstVar  = $firstVar\n");

    print("secondSub: secondVar = $secondVar\n\n");



    $firstVar  = "ccccC";

    $secondVar = "DDDDD";



    print("secondSub: firstVar  = $firstVar\n");

    print("secondSub: secondVar = $secondVar\n\n");

}

This program prints:

firstSub: firstVar = AAAAA

firstSub: secondVar = BBBBB



secondSub: firstVar  = AAAAA

Use of uninitialized value at test.pl line 19.

secondSub: secondVar =



secondSub: firstVar  = ccccC

secondSub: secondVar = DDDDD



firstSub: firstVar  = ccccC

firstSub: secondVar = BBBBB

The output from this example shows that secondSub() could not access the $secondVar variable that was created with my() inside firstSub(). Perl even prints out an error message that warns about the uninitialized value. The $firstVar variable, however, can be accessed and valued by secondSub().

Tip It's generally a better idea to use my() instead of local() so that you can tightly control the scope of local variables. Think about it this way-it's 4:00 in the morning and the project is due. Is that the time to be checking variable scope? No. Using my()enforces good programming practices and reduces headaches.

Actually, the my() function is even more complex than I've said. The easy definition is that it creates variables that only the current function can see. The true definition is that it creates variables with lexical scope. This distinction is only important when creating modules or objects, so let's ignore the complicated definition for now. You'll hear more about it in Chapter on Perl Modules.

If you remember, I mentioned calling parameters by reference. Passing parameters by reference means that functions can change the variable's value, and the main program sees the change. When local() is used in conjunction with assigning the @_ array elements to scalars, then the parameters are essentially being called by value. The function can change the value of the variable, but only the function is affected. The rest of the program sees the old value.


next up previous contents
Next: Using a List as Up: Functions Previous: Passing Parameters by Reference
dave@cs.cf.ac.uk