Viki@Stag has asked for the wisdom of the Perl Monks concerning the following question:

While writing a script to scan folder paths for certain file type, I had a problem. Maybe I will write the script first:
use File::Find; open(INLIST, $ARGV[0]) or die "cannot open $ARGV[0]: $!\n"; my @all_paths = <INLIST>; close INLIST; chomp @all_paths; foreach my $path(@all_paths) { if (-f $path && $path=~/\.dirlist$/){ open (LIST, $path) or do { print "cannot open $path: $!\n"; n +ext; }; my @temp = <LIST>; chomp @temp; push @all_paths, @temp; close LIST; next; } elsif (-d $path) { find(\&scan_path_n_rename_file, $path); } }
THe actually reads a file that has list of paths, scans each path & renames files that has certain extension to .bak
The list file can contain a path to another link file...
so i used to check this condition in my foreach & push the links from @temp to @all_paths.

This worked... I want to know why and how this works???
How is it possible to modify a variable that is already being used (ex: @all_paths used in foreach)
Why can i still use the updated variable (@all_paths gets updated by push & the script scans those paths also...)

Thanks


Replies are listed 'Best First'.
Re: How & why does this work?
by almut (Canon) on Jul 11, 2008 at 13:33 UTC

    This works because of an optimisation to use the original array directly, when you're iterating over a single array. Otherwise, a list of the items to iterate over (referencing the original elements) is internally being created on the stack.  Try adding an empty list like this

    foreach my $path (@all_paths, ()) {

    and you'll notice that the "live-update" feature stops working.

    That said, I'd like to point out that it's generally not a good idea to rely on such undocumented, implementation dependent behaviour. Also, even though it does work in this particular case, there are several other similar cases where modifying the array you're iterating over (with foreach) will produce unexpected behaviour...

      I agree. Instead of iterating over an array you modify this way, do it another way:

      while ( defined( my $path = shift @all_paths ) ) { # ...
      Update: ...but be aware that this leaves @all_paths empty.
        the $path (in my script's foreach) will be a reference (?)

        Not a reference in the normal Perl sense (i.e. you don't need to dereference it), but an alias to the original element (which at a lower level could be thought of as kind of a reference). In other words, modifying $path will modify the original value in the array. This applies in both cases, however (with and without optimisation), because the aliases are being set up to the individual elements, even when the intermediate list on the stack is being created.