Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8059717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:45:33+00:00 2026-06-05T09:45:33+00:00

What I am trying to do is get my program to recurse through a

  • 0

What I am trying to do is get my program to recurse through a directory and for all of those files within the directory, search each file for the word “ERROR” and then print the instance of it out in a seperate file. I was able to do this without making it recursive, i.e. just entering which files to check manually in the cmd. I was wondering what the proper way to use ARGV when recursing is. Here is my code thus far:

#!/usr/bin/perl
use strict;
use warnings;
use File::Find;

my $dir = "c:/programs";

find(\&searchForErrors, $dir);

sub searchForErrors()
{
my $seen = 0;

if (-f){
    my $file = $_;
    my @errors = ();

    open FILE, $file;
    my @lines = <FILE>;
    close FILE;

    for my $line (@lines){
        if (/ERROR/ ){
        push(@errors, "ERROR in line $.\n");
        print FILE "ERROR in line $.:$1\n" if (/Error\s+(.+)/);
    }
    open FILE, ">$file";
    print FILE @lines;
    close FILE;
    }
}
}

What I need to know is how I can incorporate ARGV so that the program will read in each file in the directory, perform the search, and then output the results of the search to a file. I hope I have explained my question adequately, if you need any clarification, let me know what is confusing. The more explanation you can give with the answer, the better. Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T09:45:34+00:00Added an answer on June 5, 2026 at 9:45 am

    ARGV is usually used to iterate over files provided from outside of Perl.

     $ find -type f --exec perl script.pl {} +
    
     # script.pl
     while (<>) {
         print "$ARGV:$.: $1\n" if /Error\s+(.+)/;
     } continue {
         close(ARGV) if eof;  # Reset $.
     }
    

    But it’s not necessary. You could also do:

     use File::Find::Rule qw( );
    
     @ARGV = File::Find::Rule->file->in('.');
     while (<>) {
         print "$ARGV:$.: $1\n" if /Error\s+(.+)/;
     } continue {
         close(ARGV) if eof;  # Reset $.
     }
    

    I prefer File::Find::Rule, but you could stick with File::Find for reasons that should be obvious if you compare the above snippet with the following snippet:

    use File::Find qw( find );
    
    @ARGV = ();
    find({ wanted => sub { push @ARGV, $_ if -f }, no_chdir => 1 }, '.');
    
     while (<>) {
         print "$ARGV:$.: $1\n" if /Error\s+(.+)/;
     } continue {
         close(ARGV) if eof;  # Reset $.
     }
    

    PS – You’re replacing each file with an exact copy of itself, and you’re populating an array you never use. I omitted that code from my version.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get my program to loop through the file, taking in a
I'm trying to get my program to automatically associate certain file extensions to be
I am trying to get a program to let a user enter a word
I'm trying to get my c++ program to open an sql file in notepad++.
I'm trying to get a Yii program running, but I get the error: Application
I've been trying to get a program's STDOUT redirecting to a file. So far,
I am trying to get Windows to recognise my program's File extensions but without
I am trying to get the program to call up the current date, add
i'm trying to get this program to break down a user defined amount of
I'm trying to get this simple program to work on windows, but it crashes:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.