The Perl You Need to Know, Part II: Working with Nested Subroutines Page 3

By Stas Bekman (Send Email)
Posted Sep 1, 2000

And execute it:

  % ./
  Variable "" will not stay shared at ./ line 10 (#1)
    (W) An inner (nested) named subroutine is referencing a lexical
    variable defined in an outer subroutine.
    When the inner subroutine is called, it will probably see the value of
    the outer subroutine's variable as it was before and during the
    *first* call to the outer subroutine; in this case, after the first
    call to the outer subroutine is complete, the inner and outer
    subroutines will no longer share a common value for the variable.  In
    other words, the variable will no longer be shared.
    Furthermore, if the outer subroutine is anonymous and references a
    lexical variable outside itself, then the outer and inner subroutines
    will never share the given variable.
    This problem can usually be solved by making the inner subroutine
    anonymous, using the sub {} syntax.  When inner anonymous subs that
    reference variables in outer subroutines are called or referenced,
    they are automatically rebound to the current values of such
  5^2 = 25
  6^2 = 25

Well, now everything is clear. We have the inner subroutine power_of_2() and the outer subroutine print_power_of_2() in our code.

When the inner power_of_2() subroutine is called for the first time, it sees the value of the outer print_power_of_2() subroutine's variable. On subsequent calls the inner subroutine's variable won't be updated, no matter what new values are given to in the outer subroutine. There are two copies of the variable, no longer a single one shared by the two routines.

The Remedy

The diagnostics pragma suggests that the problem can be solved by making the inner subroutine anonymous.

An anonymous subroutine can act as a closure with respect to lexically scoped variables. Basically this means that if you define a subroutine in a particular lexical context at a particular moment, then it will run in that same context later, even if called from outside that context. The upshot of this is that when the subroutine runs, you get the same copies of the lexically scoped variables which were visible when the subroutine was defined. So you can pass arguments to a function when you define it, as well as when you invoke it.

Let's rewrite the code to use this technique:
  use strict;
  sub print_power_of_2 {
    my  = shift;
    my  = sub {
      return  ** 2;
    my  = &();
    print "^2 = \n";

Now contains a reference to an anonymous function, which we later use when we need to get the power of two. (In Perl, a function is the same thing as a subroutine.) Since it is anonymous, the function will automatically be rebound to the new value of the outer scoped variable , and the results will now be as expected.

Let's verify:

  % ./
  5^2 = 25
  6^2 = 36

So we can see that the problem is solved.

When You Cannot Get Rid of The Inner Subroutine

Comment and Contribute

Your name/nickname

Your email

(Maximum characters: 1200). You have characters left.