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 8566285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:37:15+00:00 2026-06-11T17:37:15+00:00

I am doing the below steps: Read all the text files in a directory

  • 0

I am doing the below steps:

  • Read all the text files in a directory and store it in an array named @files
  • Run a foreach loop on each text file.
    Extract the file name(stripping of .txt) using split operation and creating a folder of that particular filename.
    Rename that file to Test.txt (so as to work as input fo another perl executable)
    Executing test.pl for each file by adding the line require “test.pl”;

It works fine for only one file, but not any more.
Here is my code:

opendir DIR, ".";
my @files = grep {/\.txt/} readdir DIR;

foreach my $files (@files) {

    @fn = split '\.', $files;
    mkdir "$fn[0]" 
        or die "Unable to create $fn[0] directory <$!>\n";

    rename "$files", "Test.txt";
    require "test3.pl";

    rename "Test.txt", "$files";
    system "move $files $fn[0]";
}

Any help would be very grateful.

  • 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-11T17:37:16+00:00Added an answer on June 11, 2026 at 5:37 pm

    The require function trys to be smart and load the code only if it isn’t already loaded. Why is this great? (1.) We don’t have C’s preprocessor hell with conditional includes, and (2.) we save time.

    To execute another perl script, we have a variety of possibilites. The one that you probably want, is doing a script. The do FILENAME syntax is just like eval, except that your scope isn’t visible to the done file.

    You could also start another interpreter via system "./test3.pl".

    You could make test3.pl a module, e.g. with package test3;, and pack the contents into a sub. Instead of hardcoding a filename, you would probably pass the current filename as an argument. This is not only better coding practice, but allows you to scale your application more easily, e.g. going multithreaded.

    Here is how I’d implement that snippet:

    use test3; # load the file once
    
    foreach my $file (glob "*.txt") {   # use `glob` to get a list of matching filenames
    
        (my $newdir) = $file =~ m/^ (.+) \.txt $/x;  # what you probably wanted
        mkdir $newdir 
            or die "Unable to create $newdir directory <$!>\n";
    
        test3::the_sub($file); # do the action on $file, without renaming circus.
        system "move $file $newdir"; # rather use File::Copy
    }
    

    Interesting side points:

    glob is great, works just as in the shell.

    Always use my for variables, unless you have a really good reason.

    I further assume that the file text.en.txt should not create the directory text, but text.en, and that the file .txt does not exist.

    Whether this does or doesn’t work also depends on the script you are calling.

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

Sidebar

Related Questions

I'm doing a bind to document like below, $(document).bind('click',doThis); But i have a text
Doing the below will reproduce my problem: New WPF Project Add ListView Name the
I am doing the below test to try and learn more about LINQ to
I have a method below doing casting on a String according to the given
I am learning C#. Doing the below exercises to make use of features. I
In C# 3.0, I'm doing the code below to declare a DateTime property and
What am i doing wrong in the below regular expression matching >>> import re
I'm doing a homework project that requires this: Below you will find the code
Can somebody explain to me what the below code is doing. before and after
My code below won't compile. What am i doing wrong? I'm basically trying to

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.