SHARE
Facebook X Pinterest WhatsApp

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

Written By
thumbnail Stas Bekman
Stas Bekman
Jul 20, 2010
ServerWatch content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More



By the way, the warning we saw before has gone, and so has the problem,
since there is no my() (lexically defined) variable used in the nested subroutine.

Another approach is to use fully qualified variables. This is better, since
less memory will be used, but it adds a typing overhead:

  multirun2.pl
  -----------
  #!/usr/bin/perl -w
  
  use strict;
  
  for (1..3){
    print "run: [time sh]n";
    run();
  }
  
  sub run {
  
    ::counter = 0;
  
    increment_counter();
    increment_counter();
  
    sub increment_counter{
      ::counter++;
      print "Counter is equal to ::counter !n";
    }
  
  } # end of sub run

You can also pass the variable to the subroutine by value and make the
subroutine return it after it was updated. This adds time and memory
overheads, so it may not be good idea if the variable can be very large, or
if speed of execution is an issue.

Don’t rely on the fact that the variable is small during the development of
the application, it can grow quite big in situations you don’t expect. For
example, a very simple HTML form text entry field can return a few
megabytes of data if one of your users is bored and wants to test how good
your code is. It’s not uncommon to see users copy-and-paste 10Mb core dump
files into a form’s text fields and then submit it for your script to
process.

  multirun3.pl
  -----------
  #!/usr/bin/perl -w
  
  use strict;
  
  for (1..3){
    print "run: [time sh]n";
    run();
  }
  
  sub run {
  
    my  = 0;
  
     = increment_counter();
     = increment_counter();
  
    sub increment_counter{
      my  = shift;
  
      ++;
      print "Counter is equal to  !n";
  
      return ;
    }
  
  } # end of sub run

Finally, you can use references to do the job. The version of
increment_counter() below accepts a reference to the
variable and increments its value after first dereferencing it. When you
use a reference, the variable you use inside the function is physically the
same bit of memory as the one outside the function. This technique is often
used to enable a called function to modify variables in a calling function.

  multirun4.pl
  -----------
  #!/usr/bin/perl -w
  
  use strict;
  
  for (1..3){
    print "run: [time sh]n";
    run();
  }
  
  sub run {
  
    my  = 0;
  
    increment_counter($counter);
    increment_counter($counter);
  
    sub increment_counter{
      my  = shift;
  
      15080r_counter++;
      print "Counter is equal to 15080r_counter !n";
    }
  
  } # end of sub run

Here is yet another and more obscure reference usage. We modify the value
of inside the subroutine by using the fact that variables in @_ are aliases for the actual scalar parameters. Thus if you called a function
with two arguments, those would be stored in
sh[0] and sh[1]. In particular, if an element sh[0] is updated, the corresponding argument is updated (or an error occurs if it
is not updatable as would be the case of calling the function with a
literal, e.g. increment_counter(5)).

  multirun5.pl
  -----------
  #!/usr/bin/perl -w
  
  use strict;
  
  for (1..3){
    print "run: [time sh]n";
    run();
  }
  
  sub run {
  
    my  = 0;
  
    increment_counter();
    increment_counter();
  
    sub increment_counter{
      sh[0]++;
      print "Counter is equal to sh[0] !n";
    }
  
  } # end of sub run
thumbnail Stas Bekman

Stas Bekman is a ServerWatch contributor.

Recommended for you...

What Is a Container? Understanding Containerization
What Is a Print Server? | How It Works and What It Does
Nisar Ahmad
Dec 8, 2023
What Is a Network Policy Server (NPS)? | Essential Guide
Virtual Servers vs. Physical Servers: Comparison and Use Cases
Ray Fernandez
Nov 14, 2023
ServerWatch Logo

ServerWatch is a top resource on servers. Explore the latest news, reviews and guides for server administrators now.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.