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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:13:30+00:00 2026-06-15T01:13:30+00:00

I am trying to write a simple perl script that will iterate through the

  • 0

I am trying to write a simple perl script that will iterate through the regular files in a directory and calculate the total size of all the files put together. However, I am not able to get the actual size of the file, and I can’t figure out why. Here is the relevant portion of the code. I put in print statements for debugging:

$totalsize = 0;
while ($_ = readdir (DH)) {
   print "current file is: $_\t";
   $cursize = -s $_;           
   print "size is: $cursize\n";
   $totalsize += $cursize;
}  

This is the output I get:

current file is: test.pl    size is: 
current file is: prob12.pl  size is: 
current file is: prob13.pl  size is: 
current file is: prob14.pl  size is: 
current file is: prob15.pl  size is: 

So the file size remains blank. I tried using $cursize = $_ instead but the only effect of that was to retrieve the file sizes for the current and parent directories as 4096 bytes each; it still didn’t get any of the actual file sizes for the regular files.

I have looked online and through a couple of books I have on perl, and it seems that perl isn’t able to get the file sizes because the script can’t read the files. I tested this by putting in an if statement:

print "Cannot read file $_\n" if (! -r _);

Sure enough for each file I got the error saying that the file could not be read. I do not understand why this is happening. The directory that has the files in question is a subdirectory of my home directory, and I am running the script as myself from another subdirectory in my home directory. I have read permissions to all the relevant files. I tried changing the mode on the files to 755 (from the previous 711), but I still got the Cannot read file output for each file.

I do not understand what’s going on. Either I am mixed up about how permissions work when running a perl script, or I am mixed up about the proper way to use -s _. I appreciate your guidance. Thanks!

  • 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-15T01:13:31+00:00Added an answer on June 15, 2026 at 1:13 am

    If it isn’t just your typo -s _ instead of the correct -s $_ then please remember that readdir returns file names relative to the directory you’ve opened with opendir. The proper way would be something like

    my $base_dir = '/path/to/somewhere';
    opendir DH, $base_dir or die;
    while ($_ = readdir DH) {
      print "size of $_: " . (-s "$base_dir/$_") . "\n";
    }
    closedir DH;
    

    You could also take a look at the core module IO::Dir which offers a tie way of accessing both the file names and the attributes in a simpler manner.

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

Sidebar

Related Questions

I'am trying to write simple script that will get files name from one folder
I am trying to write a simple Perl script that reads a *.csv, places
I'm trying write a simple perl script that reads some fields from a password
I am trying to write a simple Java function that will take a list
I'm trying to write a script that will grab logs across a network and
I am trying to write a simple perl script to apply a given regex
I'm trying to write simple DAO that will create entity objects based on their
I'm trying to create a simple cgi perl script that counts down from 10
I am trying to write some simple code which will read a text file
I am trying to write simple mysql stored procedure and it seems that I

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.