dcsimg

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

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


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



Comment and Contribute

Your name/nickname

Your email

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