Some comments:
- I prefer to almost always use a defined "my $variable" in a loop, especially when using nested loops each of which use $_. Doing otherwise can lead to problems. Using a "my" variable instead of $_ is very "cheap" execution time wise.
- Normally assigning $_ to a variable is not needed. Do that as part of the loop setup.
- In the "die" message, a trailing "\n" will suppress the Perl line number upon which the error occurred. For code like this, normally you will want a direct reference to the line number where the open failed, so don't put a trailing "\n". In some user programs, maybe the actual line number is confusing and you want a \n to suppress that. In any event, be aware of this difference.
- Normally do not put a trailing "/" at the end of the directory name. This can at least confuse Windows on occasion. Prepend a "/" when expanding the path, "$dir/$sub_dir/$file".
- Of course, readdir() returns just a name, not the full path, so that needs to be corrected in the open().
I think this should work, (untested):
my $dir = "directory"; #### no trailing "/"
opendir ( DH, $dir ) || die "Cannot open $dir: $!"; #### no "\n"
foreach my $file ( readdir DH) # Note: only name, not full path
{
next unless $file =~ /\.fa$/;
open (READ, '<', "$dir/$file") || die "Cannot open $dir/$file: $!";
+
while (my $line =<READ>)
{
if ($line =~ /^>/)
{
print $line;
}
}
close READ;
}
close (DH);
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.