I just read Exegesis 4, and I have a few questions about private
variables.  First, is it possible to have 2 private variables of the same
name in different functions?  For instance, what would happen in the
following code?


sub func1() {
    our $varname is private \\= 1;
    return $varname;
}

sub func2() {
    our $varname is private \\= 2;
     return varname;
}

print func1();
print func2();


Second, in the above code, the "\\=" operator is used to initialize the
private variable.  This allows it to be initialized the first time the
function is called, but prevents it from being reinitialized subsequent
times as long as the variable never becomes undefined.  But what if the
variable can become undefined?  In that case, the next time you call the
function the variable will be reinitialized, whether you want it to or not.

   Both of these show that "private" variables in Perl6 do not quite replace
C or C++ static variables.  It would be nice if there was some way to do
something like this:

my $foo is static = 3;

so that $foo acts like a C++ static variable, i.e. its value always persists
between calls to the function containing it, and the initialization code is
performed only once.  Unfortunately, I have no idea how  this would be
implemented in Perl 6.

Joe Gottman





Reply via email to