in reply to Re^2: I need help with opening a directory and reading files in that directory.
in thread I need help with opening a directory and reading files in that directory.

So, I'm assuming that you can replace the $_ default variables with any name that you want.

Yes. But let's be clear. The name of the variable is kind of irrelevant here. The contents of the variable hold the name of the file being processed, which is probably more interesting to you.

Most solutions, including the one presented to you, will loop through a list of data (like names of files) and then assign the different data to the same variable each time through the loop. Perl provides the default variable $_, but you can declare a variable of your own with your own name if you wish (I'm not sure this is what you meant, but maybe it was).

A good rule of thumb is that if you need to write out $_, to be clear, you should assign a variable name. Usually you don't need to write it out as it is implicitly handed to functions, e.g. as in:

while (<$FH>) { print; # prints the current line using $_ implicitly }
But if you need to or want to use a variable name of your choice, you can. (Also recommended is the 3-argument form of open):
while ( my $filename = readdir(DH) ) { next if $filename eq '.' or $filename eq '..'; print $filename; if ( -f $filename ) { open my $FH, '<', $filename or die "open: $!\n"; while ( my $line = <$FH> ) { print $line; } close FH or die "close: $!\n"; } }
But the "any name you want" applies to the variable ... if you are looking for a particular name, as in a piece of data, you'll have to loop through and find it. While you can certainly say:
if ( -f 'The Bravery-Believe.txt' ) { # do something }
. . . it wouldn't make sense to do that in a loop through a list, as you'd be asking each element of the list about a certain other element, when they don't "know anything about each other."

What you might do is check to see if you are dealing with the file you want and skip the rest: then you would have use for the specific filename, which is what I think you might be getting at:

my $wanted = 'The Bravery-Believe.txt'; while ( my $filename = readdir(DH) ) { next unless $filename eq $wanted; # do something . . . }
Hope this helps build on shadowsong's answer.

The way forward always starts with a minimal test.