in reply to Re: 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.

Thanks shadowsong. I was looking through the perldocs and did see a lot of the information that you presented. It does help to get others opinion on this because the instructor that I have was telling me to us @ARGV to open the directory. That confused me even more because I had just did another assignment that parsed two files and did something with those files. But, the files were local to the .pl file I wrote. So, you could get away with that.

I figured that the glob or opendir would work just you look stated and based on the research I did on the internet and this site. So, I'm assuming that you can replace the $_ default variables with any name that you want. I do like how you explained the information on using stat info to see if it really is a file that we want to look at. I will get to work on fixing what I have created and I will let you guys know how it goes.

Thanks, Brandon

  • Comment on Re^2: I need help with opening a directory and reading files in that directory.

Replies are listed 'Best First'.
Re^3: I need help with opening a directory and reading files in that directory.
by 1nickt (Canon) on Sep 06, 2015 at 14:27 UTC

    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.