Part of the piece of code I'm working on involves getting a list of valid directories from the user. The problem that I'm having is this...
The code below works, but I get a error message stating.
Variable "@dirlist" will not stay shared at test.pl line 17.
Variable "@dirlist" will not stay shared at test.pl line 24.
Where/how do I declare that variable to avoid that warning? If I place the "my @dirlist" within the "getAns()" sub, it redeclares the variable and erases any information that was added. If I leave it out of that sub, I get that warning.
#!/usr/bin/perl -w
use strict;
my @dirs = &getDirs();
sub getDirs() {
my @dirlist; # PROBLEM!!!
sub getAns() {
print qq(Enter a valid directory: );
chomp(my $ans = <STDIN>);
if ($ans ne "") {
if ($ans =~ m/foo/g) {
push(@dirlist, $ans);
&getAns;
} else {
print qq(That is not a valid directory!\n);
&getAns;
}
} else {
return @dirlist;
}
}
}
my @dirs = &getAns();
if (@dirs) {
return @dirs;
} else {
die qq(No directories have been entered!\n);
}
}
Thanks in advance for the help,
Rich36