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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:40:27+00:00 2026-06-10T01:40:27+00:00

I am trying to implement a procedure that iterates over all the subdirectories from

  • 0

I am trying to implement a procedure that iterates over all the subdirectories from a root directory, looking for .xml files in Perl.

sub getXMLFiles {

    my $current_dir = $_[ 0 ];
    opendir my $dir, $current_dir or die "Cannot open directory: $!\n";
    my @files = grep /\.xml$/i, readdir $dir;
    closedir $dir;

    return @files;
}

sub iterateDir {
    my $current_dir = $_[ 0 ];
    finddepth( \&wanted, $current_dir );
    sub wanted{ print getXMLFiles }
}
#########################################################
#                                                       #
# define the main subroutine.                           #
# first, it figures from where it is being ran          #
# then recursively iterates over all the subdirectories #
# looking for .xml files to be reformatted              #
#                                                       #
#########################################################
sub main(){
    #
    # get the current directory in which is the 
    # program running on
    #
    my $current_dir = getcwd;
    iterateDir( $current_dir );
}

#########################################################
#                                                       #
# call the main function of the program                 #
#                                                       #
#########################################################
main();

I’m not very familiar with Perl. The sub iterateDir procedure is supposed to iterate over a the sub-directories, while the getXMLFiles would filter for .xml files, returning them. I would use those .xml files for parsing. That’s why I’m trying to find all the .xml files from a root directory.
However, I don’t know how can I use the sub wanted procedure inside iterateDir to send the dirpath to getXMLFiles. How could I accomplish that?

  • 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-10T01:40:29+00:00Added an answer on June 10, 2026 at 1:40 am

    $File::Find::dir is the current directory name. You can use that variable in the wanted sub and pass it to subs you call. See the docs for more info on the wanted function

    This should work:

    sub iterateDir {
        my $current_dir = $_[ 0 ];
        finddepth( \&wanted, $current_dir );
        #                                    |
        # pass current dir to getXMLFiles    V
        sub wanted{ print getXMLFiles($File::Find::dir) }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

pstrjds, From the code you have provided I am trying to implement that code
I'm trying to implement optional parameters in a stored procedure that I have but
I'm trying to have all my views inherit from a custom class so that
I am trying to implement a function on jquery datatable, that returns the 1st
I'm trying to implement the blog this function from Flickr using the BloggerAPI to
I'm trying to implement a flex app that will be able to record a
SetFocus I'm trying implement the above Se Focus code in a Class Library that
I am trying to create a stored procedure that takes a bit parameter which
I am trying to implement MoveItemUp and MoveItemDown methods that move a selected row
I was trying to implement Fermat's primality test in Scheme. I wrote a procedure

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.