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

I don't understand how this code:
my $directory = File::Spec->catfile(my @path, my $directoryTest); opendir DIR,my $directory or die "cannot open dir $directory: $!"; my @file= readdir DIR; my @FinalDirectoryList; push (my @FinalDirectoryList,my @file);
Gives this error ""my" variable @file masks earlier declaration in same scope at /root/begperl/DirectorySearch.plx line 20" I understand what the error means but "my @file" only appears 2x in the program. There are several more like this. Full code below.
#!/usr/bin/perl use strict; use warnings; use File::Spec::Functions; print "First Box in Disk?\n"; my $firstBox = <stdin>; print "Last Box in Disk?\n"; my $lastBox = <stdin>; print "Pick Directory\n"; my $directoryTest = <stdin>; my @path="/root/begperl/TestDirectory/"; my $directory = File::Spec->catfile(my @path, my $directoryTest); opendir DIR,my $directory or die "cannot open dir $directory: $!"; my @file= readdir DIR; my @FinalDirectoryList; push (my @FinalDirectoryList,my @file); foreach my $file (@file) {print "$file\n";} close FileNameList; #close the file. closedir DIR; #close directory

Replies are listed 'Best First'.
Re: Perl Errors
by toolic (Bishop) on Mar 27, 2012 at 20:01 UTC
    To eliminate those warning messages, only declare each variable with my once. For example, change:
    my $directory = File::Spec->catfile(my @path, my $directoryTest);

    to:

    my $directory = File::Spec->catfile(@path, $directoryTest);

    This compiles without warnings for me:

    use strict; use warnings; use File::Spec::Functions; print "First Box in Disk?\n"; my $firstBox = <stdin>; print "Last Box in Disk?\n"; my $lastBox = <stdin>; print "Pick Directory\n"; my $directoryTest = <stdin>; my @path="/root/begperl/TestDirectory/"; my $directory = File::Spec->catfile(@path, $directoryTest); opendir DIR, $directory or die "cannot open dir $directory: $!"; my @file= readdir DIR; my @FinalDirectoryList; push (@FinalDirectoryList, @file); foreach my $file (@file) {print "$file\n";} closedir DIR; #close directory

    diagnostics will give you a more verbose explanation of the warning messages.

      Ok so to be clear. When I initialize a variable use my but I don't have to use my every time I call it?
        Correct! Every time you use my $variable a new $variable is created that will hide the previously declared variable.

        If you declare the second $variable in the same scope, you cannot access the value of the previously declared variable anymore.

        If you declare the second $variable in a different scope, you cannot access the value of the previously declared variable while you are in that scope. Leaving that scope, the earlier variable will suddenly re-appear. As you will understand, this can cause all kinds of weirdness and confusion, so don't do it unless you have a very good reason for it.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        My blog: Imperial Deltronics

        Hellhound4:

        Yes. You use my to declare a new variable in a scope (file, curly braces). So this:

        my $fruit = 'apple'; { my $fruit = 'banana'; print "$fruit\n"; } print "$fruit\n";

        should print apple and banana, because you have *two* variables named $fruit. While you're within the curly braces, the second one masks (hides) the first one. If you remove the second 'my', then it should print banana and banana, since you're using the same variable.

        Note: I didn't test the code.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.