How Do You Give Functions Private Variables That Retain Their Values Between Calls

How Do You Give Functions Private Variables That Retain Their Values Between Calls


  • 18 Dec, 2011

    Create a scope surrounding that sub that contains lexicals.Only lexical variables are truly private, and they will persist even when their block exits if something still cares about them. Thus:{ my $i = 0; sub next_i { $i++ } sub last_i { --$i } }creates two functions that share a private variable. The $i variable will not be deallocated when its block goes away because next_i and last_i need to be able to access it.

    Comments Received:
    Please give your suggestions and feedback:

2009-2016 downloadmela.com. All rights reserved.