Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

It's impossible to access the contents of a file without opening it. Sometimes you don't need to _explicitly_ open the files as a library or the operating system will do that for you, but if you're accessing the contents of a file in any way, then that file _is_ being opened by someone.

Looking at your code, it seems you've been confused by the fact that Perl's grep function doesn't work the same way as the Unix grep command. The Unix command works on a list of filenames (Unix opens the files and searches them for you) but the Perl function works on a list of strings. The Perl function is actually far more powerful and flexible than the Unix command. In fact, using it to emulate the Unix command is probably the one thing that it's a bit clunky at :-)

So your best solution is to do it the obvious way. Open each file and read it to see if it contains the test you're looking for. You could probably do something clever with @ARGV and <>, but I don't think you'd gain much.

Update: Actually, I probably wouldn't use grep in this case. The trouble with grep is that it always checks the whole list. And with a large list where your search term appears near the beginning, that can be inefficient. I'd do something like this:

sub contains { my ($file, $search) = @_; open my $fh, '<', $file or die $!; while (<$fh>) { return 1 if /$search/; } return; } my @files = grep { contains($_, $search_str) } @arr1;

In reply to Re: search for a pattern in file without opening the file by davorg
in thread search for a pattern in file without opening the file by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-19 04:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found