Guides The Perl You Need to Know, Part II: Working with Nested Subroutines...

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




And execute it:

  % ./nested.pl
  
  Variable "" will not stay shared at ./nested.pl 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
    variables.
    
  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:

  anonymous.pl
  --------------
  #!/usr/bin/perl
  
  use strict;
  
  sub print_power_of_2 {
    my  = shift;
  
    my  = sub {
      return  ** 2;
    };
  
    my  = &();
    print "^2 = n";
  }
  
  print_power_of_2(5);
  print_power_of_2(6);

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:

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

So we can see that the problem is solved.

When You Cannot Get Rid of The Inner Subroutine

Latest Posts

Get-MsolUser PowerShell Attributes & Properties

This article has been updated for 2020. Please note that WAAD was retired in 2018, but the cmdlets listed in this article are still...

Microsoft Azure PowerShell Scripts and Commands

Using PowerShell scripts and commands for quickly executing tasks in Windows operating systems offers a number of benefits over traditional scripting languages, such as...

Microsoft Hyper V Review

Microsoft Hyper-V: The Bottom line Microsoft Hyper-V lagged behind VMware's virtualization tool, one of the most popular tools in the space, when it was first...

Best Cloud Based Services & Companies

Any company that’s delayed introducing cloud-based software into their infrastructure needs to consider leveraging these new technologies to reap all the benefits cloud computing...

Best Load Balancers for 2020

The best load balancers are able to provide either hardware or software-based load balancing to ensure maximum service availability by offering network traffic distribution...

Related Stories