Servers 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 6




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

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