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.

In reply to Re^3: I need help with opening a directory and reading files in that directory. by 1nickt
in thread I need help with opening a directory and reading files in that directory. by brawal128

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.