Bootstrap FreeKB - Perl (Scripting) - Resolve "Variable will not stay shared"
Perl (Scripting) - Resolve "Variable will not stay shared"

Updated:   |  Perl (Scripting) articles

Let's say you have the following Perl script.

#!/usr/bin/perl
use strict;
use warnings;

sub outer {
  my $foo = "bar";
  sub inner {
    $foo = "world";
  }
}

 

Running this script should return the following.

Variable "$foo" will not stay shared at /home/c065234/testing.pl line 25.

 

In this example, this is occuring with the $foo variable in the inner subroutine. One solution would be to redefine the $foo variable in the inner subroutine using my or our.

#!/usr/bin/perl
use strict;
use warnings;

sub outer {
  my $foo = "bar";
  sub inner {
    my $foo = "world";
  }
}

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter b719e6 in the box below so that we can be sure you are a human.