While working on a script to transfer some legacy data from flat files to a db I encountered a strange problem.
An array which was holding directory names somehow acted as if it was empty in one part of the script yet the variable couldnt have been out of scope.
The problem seems to have been caused by me initialising the variable my @dirs=''; When changed to my @dirs=(); it worked ok.
I cant figure out why this would cause the problem. Does anybody have any ideas?
Here is an extract of the offending script:
my @dirs='';
my $dir='/usr/local/apache/cgi-bin/users';
my $choice='';
title();
#get user directories
#get all the files in the directory
opendir DIR, "$dir";
my @directory=grep(!/^\.\.?$/, readdir DIR); # remove . and .. entri
+es
closedir DIR;
# get only directories not files
foreach (@directory){
my path=$dir.'/'.$_;
if (-d $path){
push @dirs,$_;
}
}
# here i prove to myself that the array does contain values
foreach (@dirs){
print "$_\n";
}
#program flow
while($choice ne 'x'){
if ($choice ne 'x'){
menu();
print "\nPlease enter an option =>";
$choice=chomp(<STDIN>);
}
if ($chioce eq '1'){
# parse user details file
$no_users=0;
foreach (@dirs){
my $path=$dir.'/'.$_;
my $filename=$path.'/'.$_.'_details';
print "$_\n"; # this prints nothing!!!!
# rest of scripr........etc
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.