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



In reply to How & why does this work? by Viki@Stag

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.