Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: Array contents vanishing...why is this???
by derby (Abbot) on May 30, 2002 at 13:16 UTC
    There's nothing related to @dirs that I see wrong with this snippet of code ( but $choice = chomp(<STDIN>); should be chomp( $choice = <STDIN> ); ). I ran the snippet fine with the chomp change (and menu() and title() commented out). Is there something in the menu function that is clobbering @dirs?

    -derby

Re: Array contents vanishing...why is this???
by cLive ;-) (Prior) on May 30, 2002 at 13:20 UTC
    You seem to be missing a couple of lines from the beginning of your script :)
    #!/usr/bin/perl -w use strict;

    .02

    cLive ;-)

    --
    seek(JOB,$$LA,0);

Re: Array contents vanishing...why is this???
by Kanji (Parson) on May 30, 2002 at 14:29 UTC
    I cant figure out why this would cause the problem.

    I think you're mistaking '' as being synonymous with () when initializing an array, when what you're really doing is setting the first element to be an empty string (ie, my @dirs = ( '' );), making @dirs non-empty but giving you the appearance of nothingness as you iterate over @dirs (at least once, anyway).

    You might also want to read undef'ing @arrays caveat which covered a very similar issue.

        --k.


      Thanx guys, i think there is something unstable about the rest of the script, i'm getting other problems too.
      There is nothing in menu() or title() except print statements so they cant be the problem.
      I'm also getting readline() on closed filehandle main::FILE errors when another option is chosen from the menu. It seems to be pasting the results of the last file read into the filename of the next file :-)
      looks like i've really messed this one up :-)