in reply to Re: Compare and copy array values
in thread Compare and copy array values

If the variables are declared a the beginning of the script do I need to use "my" when referencing them further down in the script?

Replies are listed 'Best First'.
Re^3: Compare and copy array values
by Narveson (Chaplain) on Mar 19, 2008 at 21:17 UTC

    No, you don't.

    But loop variables should be declared in the loop construct:

    foreach my $item (@list) { }
    or (means the same)
    for my $item (@list) { }
      I printed some of the output because it was not working correctly and it tries to put the hash key value pair in as the directory to copy to and I just want the keys as the directory names to copy to in:

      copy($file, "$to_dir/$dir") or warn "Cannot copy '$file' to '$to_dir/$fi le' $!"
Re^3: Compare and copy array values
by wardy3 (Scribe) on Mar 19, 2008 at 22:21 UTC

    If you do re-declare them, they will override the earlier one.

    my $crap =1; print "crap is $crap\n"; my $crap =2; print "crap is $crap\n";
    produces
    crap is 1 crap is 2

    If you include use warnings; at the beginning of the scripts, you will get "my" variable $crap masks earlier declaration in same scope at crap.pl line 8. which can be very useful!

    Just keep in mind that it checks in the same scope. That means you can re-declare the same variable name inside a loop and Perl will silently accept it.

    Enjoy
    ~ Michael