If you're working in your target directory, the error message you've asked about appears because you're trying to open the parent directory (..), the current directory (.) and subdirectories, if any, as if they were files, because opendir captured those to your array of files.

Knock those out of your @files before trying to open anything. (see http://perldoc.perl.org/perlfunc.html)

You have some other problems in this script. Pay special attention to graff's discussion of path. Some will be easily solved if you add use diagnostics; to your pragmata; some are in nature of your failure to test the open at line 21 (and -- in the same line -- failure to use what's now considered best practice: 3 arg open with lexical filehandles.)

Perhaps most critical among the problems you didn't ask about is the attempt at line 23 to test an array (in scalar context) for a match -- and you need to read about qr/.../ -- either qr or in Quote and Quote-like Operators to make your regex match what you expect... and at line 24, where you'll find you're pushing something quite unexpected onto @found.

Updated for grammar, markup and clarity

Update 2 (Warning: Sunday morning content): This is one way of attacking your target (and problem) that's along the lines you initially tried:

#!/usr/bin/perl use warnings; use strict; # use diagnostics; #regexp.pl # 911425 print "Enter the full path to the directory you want to search: "; my $folder = <>; chomp $folder; chdir($folder); opendir(DIR, $folder) or die "Can't open $folder, $!"; my @files = readdir(DIR) or die "Can't readdir $folder, $!"; print "What's the phrase you're looking for?: "; my $find = <STDIN>; chomp $find; my $searchterm = qr/$find/; my (%found, $found, $file); for $file(@files) { next if ($file =~ /^\./); next unless (-T $file); # text files only (excludes binary f +iles such as *.doc or .xls) open(my $fh, '<', $file) or die "Can't open $file: $!"; my @content = <$fh>; for my $line(@content) { if ($line =~ /$searchterm/i) { my $key = $file; $found{$key} += 1; } } } while (my ($key, $value) = each %found) { print "$key has \t $value instance(s) of \t \"$find\"\n"; }

In reply to Re: Tricky regexp by ww
in thread Tricky regexp by riceboyyy

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.