Broad Network


Static or State Variable in Perl

Perl Scoping Part 3

Perl Course

Foreword: In this part of the series, I talk about the scope of the state variable modifier, in Perl.

By: Chrysanthus Date Published: 8 Nov 2015

Introduction

This is part 3 of my series, Perl Scoping. In this part of the series, I talk about the scope of the state variable modifier, in Perl. You should have read the previous parts of the series before reaching here; this is the continuation. The state variable in Perl is what is called the static variable in other languages.

Duration of a my Variable
Assume that you have a my variable inside a block. At the end of the block, the my variable is destroyed as the program runs. That is why it cannot be seen outside the block. We say the my variable is destroyed as it goes out of scope.

A state variable is a variable that is not destroyed when it goes out of scope. Also, with a state variable, if the scope (block) is executed repeatedly, initialization takes place only once, the first time, the scope is executed. I will use the function block to explain this. I will start with a normal my variable in a function and show how it dies when it goes out of scope. Then I will define a state variable in the same function and prove that it does not die, when it goes out of scope.

Normal my Variable
Consider the function, fn() in the following code:

use strict;

    sub fn()
        {
            my $n = 0;

            print $n, "\n";

            $n = $n + 2;
    }

    fn();
    fn();
    fn();
    fn();

In the function, fn(), $n is an ordinary integer variable. Below in the code, the function fn() is called 4 times. Each time the function, fn() is called, $n is reinitialized to zero.  So the value displayed for $n from fn() is always zero. After a display (print statement), $n is increased by 2, but this has no effect in the display (output), the next time fn() is called. This increase of $n and $n as a whole, die (is annulled) at the very end of the function block.

state Variable
If in the fn() function above, $n where made state (preceded by the modifier, state), then $n will be initialized only when the function is called for the first time. That is one of the characteristics of a state variable. Another characteristic of a state variable is that it does not die when it goes out of scope. So, for a state variable, if the block (scope) is re-executed, the previous value of the state variable is maintained. To declare a state variable, precede the normal declaration with the modifier, state and not with my or our. Well, you actually precede the variable with CORE::state and not just state. Read and try the following code:

use strict;

    sub fn()
        {
            CORE::state $n = 0;

            print $n, "\n";

            $n = $n + 2;
    }

    fn();
    fn();
    fn();
    fn();

In the fn() function, $n is now a static object (state variable). The first time the fn() function is called, $n is initialized to zero. Then $n is displayed. Then $n is increased by 2. and the function block goes out of scope, temporarily. Since the value of a state variable does not die when it goes out of scope, the last value of $n is retained by $n.

The next time, the function, fn(), is called, $n is no longer initialized; that is, the initialization statement is skipped. After the skipping, $n is displayed, and then $n is increased by 2 on its previous value. So, the output of the above code is, 0, 2, 4, 6, in new lines.

So, a state variable is a variable that is initialized only once, if its scope block is executed many times, and it is a variable that does not die when it goes out of scope.

The initialization of a state variable should not be broken down into declaration and assignment.

Not seen Out of Scope
A state variable cannot be seen out of its scope, even though it lives after its scope is completed. For example, in the above program, if you use the variable, $n outside the fn() function block, the program will not run. So the following program in which $n has been used outside its scope, issues an error message.

use strict;

    sub fn()
        {
            CORE::state $n = 0;

            print $n, "<br>";

            $n = $n + 2;
    }

    $n = 15; #used outside scope

    fn();
    fn();
    fn();
    fn();

That is what I have on state or static variable. I hope you appreciate it

Chrys

Related Links

Perl Basics
Perl Data Types
Perl Syntax
Perl References Optimized
Handling Files and Directories in Perl
Perl Function
Perl Package
Perl Object Oriented Programming
Perl Regular Expressions
Perl Operators
Perl Core Number Basics and Testing
Commonly Used Perl Predefined Functions
Line Oriented Operator and Here-doc
Handling Strings in Perl
Using Perl Arrays
Using Perl Hashes
Perl Multi-Dimensional Array
Date and Time in Perl
Perl Scoping
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Advanced Course
Miscellaneous Features in Perl
Perl Two-Dimensional Structures
Advanced Perl Regular Expressions
Designing and Using a Perl Module
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course

BACK

Comments

Become the Writer's Fan
Send the Writer a Message