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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.