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

  • Home
  • SEARCH
  • 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 120545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:49:42+00:00 2026-05-11T03:49:42+00:00

I have recently started learning Perl and one of my latest assignments involves searching

  • 0

I have recently started learning Perl and one of my latest assignments involves searching a bunch of files for a particular string. The user provides the directory name as an argument and the program searches all the files in that directory for the pattern. Using readdir() I have managed to build an array with all the searchable file names and now need to search each and every file for the pattern, my implementation looks something like this –

sub searchDir($) {     my $dirN = shift;     my @dirList = glob('$dirN/*');     for(@dirList) {         push @fileList, $_ if -f $_;      }     @ARGV = @fileList;     while(<>) {         ## Search for pattern     } } 

My question is – is it alright to manually load the @ARGV array as has been done above and use the <> operator to scan in individual lines or should I open / scan / close each file individually? Will it make any difference if this processing exists in a subroutine and not in the main function?

  • 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. 2026-05-11T03:49:43+00:00Added an answer on May 11, 2026 at 3:49 am

    On the topic of manipulating @ARGV – that’s definitely working code, Perl certainly allows you to do that. I don’t think it’s a good coding habit though. Most of the code I’ve seen that uses the ‘while (<>)’ idiom is using it to read from standard input, and that’s what I initially expect your code to do. A more readable pattern might be to open/close each input file individually:

    foreach my $file (@files) {     open FILE, '<$file' or die 'Error opening file $file ($!)';     my @lines = <FILE>;     close FILE or die $!;      foreach my $line (@file) {         if ( $line =~ /$pattern/ ) {             # do something here!         }     } } 

    That would read more easily to me, although it is a few more lines of code. Perl allows you a lot of flexibility, but I think that makes it that much more important to develop your own style in Perl that’s readable and understandable to you (and your co-workers, if that’s important for your code/career).

    Putting subroutines in the main function or in a subroutine is also mostly a stylistic decision that you should play around with and think about. Modern computers are so fast at this stuff that style and readability is much more important for scripts like this, as you’re not likely to encounter situations in which such a script over-taxes your hardware.

    Good luck! Perl is fun. 🙂

    Edit: It’s of course true that if he had a very large file, he should do something smarter than slurping the entire file into an array. In that case, something like this would definitely be better:

    while ( my $line = <FILE> ) {     if ( $line =~ /$pattern/ ) {         # do something here!     } } 

    The point when I wrote ‘you’re not likely to encounter situations in which such a script over-taxes your hardware’ was meant to cover that, sorry for not being more specific. Besides, who even has 4GB hard drives, let alone 4GB files? 😛

    Another Edit: After perusing the Internet on the advice of commenters, I’ve realized that there are hard drives that are much larger than 4GB available for purchase. I thank the commenters for pointing this out, and promise in the future to never-ever-ever try to write a sarcastic comment on the internet.

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

Sidebar

Ask A Question

Stats

  • Questions 73k
  • Answers 73k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Finally I've change my code for this one: procedure MyComponent.MouseMove(Sender:… May 11, 2026 at 1:59 pm
  • added an answer Lately I use Facelift as I find it a little… May 11, 2026 at 1:59 pm
  • added an answer Does your review model have the corresponding belongs_to :user belongs_to… May 11, 2026 at 1:59 pm

Related Questions

I have recently started learning F#, and this is the first time I've ever
I've been a web developer for some time now, and have recently started learning
I have recently started having problems with TortoiseCVS, or more specifically with plink, the
I have recently started using Vim as my text editor and am currently working
I recently started learning Python and I was rather surprised to find a 1000
I have a wysiwyg editor in my back end, and it is tripping up

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.