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

by
Jeremy Canfield |
Updated: July 26 2022
| 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