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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:27:43+00:00 2026-05-18T20:27:43+00:00

sub dir_list1 { $path=$_[0]; while(<$path/*>){ if (-f $_){ print $path/$_\n; } else { print

  • 0
sub dir_list1
{
    $path=$_[0];
    while(<$path/*>){
        if (-f "$_"){
            print "$path/$_\n";
        }
        else {
            print "dir: $path/$_\n";# if ($entry ne "." && $entry ne "..");
            dir_list1($_);
        }
    }
}
dir_list1(".");

When i execute the above code, it first prints ALL the contents of the current directory and then goes on to list the contents of the subdirectory. Should it not go into the sub-dir once it encounters a sub-dir, list the files inside and resume with the parent folder?

Thanks.

[Edit, in response to OrangeDog]

I’m using this code on windows. The output is something like this:
a.txt
b.txt
dir: ./images
c.txt
d.txt
…
[and then the images folder is listed]
./images/qwe.jpg
./images/asd.jpg
./images/zxc.jpg
…

  • 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-05-18T20:27:44+00:00Added an answer on May 18, 2026 at 8:27 pm

    You’ve got a bunch of problems here. Here’s a version that actually works:

    use strict;
    use warnings;
    
    sub dir_list1
    {
        my $path = $_[0];
        for (<$path/*>) {
            if (-f $_) {
                print "$_\n";
            }
            else {
                print "enter dir: $_\n";
                dir_list1($_);
                print "leave dir: $_\n";
            }
        }
    }
    dir_list1(".");
    

    Things wrong with the original code:

    • Lack of use strict; use warnings;
    • Not using a lexical variable for $path
    • Using quotes around a variable is usually redundant ("$_")
    • The filenames returned by the glob operator include the path you gave to it

    But the fundamental problem was that the glob operator in scalar context can’t be used recursively. The iterator it uses is tied to that particular line of code. When you recurse, the iterator is still returning filenames from the parent directory.

    I changed your while (scalar context) to a for (list context). A for loop generates the complete list of filenames and then iterates over it, and it can be used recursively.

    I’m assuming you’re doing this as a learning exercise. Otherwise, you ought to be using one of the many modules for finding files. Here’s a partial list:

    • File::Find – the classic, a core module since 5.000. But an annoying interface.
    • File::Find::Rule – wraps File::Find in a nicer interface
    • File::Next – has an iterator-based interface that avoids having to read the entire directory tree before returning anything
    • Path::Class::Iterator – like File::Next, but with the magic filename objects of Path::Class

    I’m sure there’s more I’ve overlooked.

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

Sidebar

Related Questions

Many beginning programmers write code like this: sub copy_file ($$) { my $from =
A python script need to spawn multiple sub-processes via fork(). All of those child
I've got a function that currently grabs all folders and sub-folders to check the
Given the URL (single line): http://test.example.com/dir/subdir/file.html How can I extract the following parts using
Given this: Public Sub timReminder_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) If DateTime.Now()
How to move a sub directory from one directory to another, given that both
I have classX: Sub New(ByVal item_line_no As String, ByVal item_text As String) ' check
i have a sub with this signature Public Sub Save(ByVal obj As IPta) now
How do I copy a directory including sub directories excluding files or directories that
Let's say that I create a Sub (not a function) whose mission in life

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.