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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:46:52+00:00 2026-06-05T15:46:52+00:00

I am newbie at Perl. I am trying to write a Perl script that

  • 0

I am newbie at Perl. I am trying to write a Perl script that prints the content of all the files in a directory and recursively do it till all the files in the directories/sub-directories are printed. I am doing it with the following subroutine.

sub print_files_in_dir {
my $file = shift;
if(-d $file){
 my @files=glob "$file/*";
  foreach my $file (@files){
   if(-d $file){
    print_files_in_dir $file;
   }
   else {
    print_file $file;
   }
 } 
 else {
  print_file $file;
 }
}

where print_file is sub which prints the content of a single file. I get this error with I try to execute this code with a directory say dir1 which contains another directory named dir2.

Can’t locate object method “print_files_in_dir” via package “dir1/dir2” (perhaps you forgot to load “dir1/dir2”?)

Cant figure out why is it throwing this error ?

  • 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-05T15:46:55+00:00Added an answer on June 5, 2026 at 3:46 pm

    As you learned, omitting parens around argument lists can give you really weird errors. Some are even silent. For example, print (4+6)*2 prints 10! (ok, it’ll warn if you have warnings on as you should, so it’s not really silent, but it’s not fatal either.) One must be careful if one is omits those parens.

    I’m writing this because I noticed a problem with your glob; it won’t work if the path has certain characters in them, such as spaces.

     glob "$file/*"
    

    should be

     glob "\Q$file\E/*"
    

    And since I’m here, there’s a bit of redundancy that can be eliminated. Your sub collapses to:

    sub print_files_in_dir {
       my $file = shift;
       if (-d $file) {
          print_files_in_dir($_) for glob "\Q$file\E/*";
       } else {
          print_file($file);
       }
    }
    

    Actually, you could use File::Find::Rule to collapse it further.

    use File::Find::Rule qw( );
    print_file($_) for File::Find::Rule->file->in('.');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a perl script to search through a text file, find
All, I realise against gooogle ToC but I was trying to write a perl
I am Newbie to Perl script. I want to do a read and write
Newbie question since I'm not up to speed using maven at all. I'm trying
(Newbie DBUnit question Alert!) It appears that DBUnit for each table 'deletes all the
Perl newbie here...I had help with this working perl script with some HASH code
Perl newbie here. I have a log file that I need to parse out
Newbie here, I am trying to use Bioperl module in the perl environment. My
I am very newbie to Perl. I wrote a very simple Perl program (script):
I'm a Perl newbie. I'm trying to learn based on a previous person's work

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.