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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:32:09+00:00 2026-05-14T06:32:09+00:00

This is a really basic issue, but I’m new to perl and cannot work

  • 0

This is a really basic issue, but I’m new to perl and cannot work out what the issue is. I’m just trying to isolate the files in a directory, but the -d operator keeps treating all the folder contents as files …

@contents is my array, and when I run this:

use strict;

if ($ARGV[1]) {
    die("Error: You can only monitor one directory at a time\n");
}

my $directory = $ARGV[0] || die "Error: No directory defined\n";

opendir(DIR, $directory) || die "Error: Can't open dir $directory: $!";
my @contents = readdir(DIR);

foreach my $item(@contents) {
    if (-d $item) { next; }
    print"$item is a file\n"; 
}

closedir (DIR);

I keep getting both folders and files. Alternatively, if I use -f, I get nothing.

edit: this is the output –

file01.txt is a file
folder 01  is a file
folder 02 is a file
Screen shot 2010-04-18 at 1.26.17 PM.png is a file

I’m running this on OSX

edit:dir ls -l output:

aaron ~/Documents/test: ls -l
total 112
-rw-r--r--@ 1 aaron  staff  51235 18 Apr 13:26 Screen shot 2010-04-18 at 1.26.17 PM.png
-rw-r--r--@ 1 aaron  staff      7 18 Apr 13:26 file01.txt
drwxr-xr-x  3 aaron  staff    102 18 Apr 13:25 folder 01
drwxr-xr-x  2 aaron  staff     68 18 Apr 13:25 folder 02
  • 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-14T06:32:10+00:00Added an answer on May 14, 2026 at 6:32 am

    Solution

    I was testing with ‘.’ as the directory…you’re testing with some other directory. The names read from the directory are then checked relative to the current directory. If I use some other directory name, I’ll get almost everything except ‘.’ and ‘..’ listed as files, regardless.

    If you prefix the name with the value of $ARGV[0], you’ll get the expected result:

    #!/bin/perl -w
    
    use strict;
    
    if ($ARGV[1]) {
        die("Error: You can only monitor one directory at a time\n");
    }
    
    my $directory = $ARGV[0] || die "Error: No directory defined\n";
    
    opendir(DIR, $directory) || die "Error: Can't open dir $directory: $!";
    my @contents = readdir(DIR);
    
    foreach my $item(@contents) {
        next if -d "$ARGV[0]/$item";
        print "$ARGV[0]/$item is a file\n"; 
    }
    
    closedir (DIR);
    

    Previous attempts to explain

    This works on MacOS X:

    #!/bin/perl -w
    use strict;
    
    my @contents = <*>;
    
    foreach my $item (@contents)
    {
        print "== $item\n";
        next if -d $item;
        print "$item is a file\n";
    }
    

    Test:

    MiniMac JL: perl -c xx.pl
    xx.pl syntax OK
    MiniMac JL: perl xx.pl
    == cproto-4.7g
    == fpqsort1
    fpqsort1 is a file
    == fpqsort1.h
    fpqsort1.h is a file
    == fpqsort2
    fpqsort2 is a file
    == fpqsort2.c
    fpqsort2.c is a file
    == gcc-predef.h
    gcc-predef.h is a file
    == git-1.6.5.7
    == go
    == makefile
    makefile is a file
    == qs-test1.c
    qs-test1.c is a file
    == qs-test2.c
    qs-test2.c is a file
    == RCS
    == rep-report.txt
    rep-report.txt is a file
    == select.c
    select.c is a file
    == soq
    == xx.pl
    xx.pl is a file
    MiniMac JL: 
    

    Given a marginally modified version of the code in the question:

    #!/bin/perl -w
    
    use strict;
    
    if ($ARGV[1]) {
        die("Error: You can only monitor one directory at a time\n");
    }
    
    my $directory = $ARGV[0] || die "Error: No directory defined\n";
    
    opendir(DIR, $directory) || die "Error: Can't open dir $directory: $!";
    my @contents = readdir(DIR);
    
    foreach my $item(@contents) {
        print "<<$item>>\n";
        next if -d $item;
        print"$item is a file\n"; 
    }
    
    closedir (DIR);
    

    Running it on the same directory as before, I get the output:

    Minimac JL: perl yy.pl .
    <<.>>
    <<..>>
    <<cproto-4.7g>>
    <<fpqsort1>>
    fpqsort1 is a file
    <<fpqsort1.h>>
    fpqsort1.h is a file
    <<fpqsort2>>
    fpqsort2 is a file
    <<fpqsort2.c>>
    fpqsort2.c is a file
    <<gcc-predef.h>>
    gcc-predef.h is a file
    <<git-1.6.5.7>>
    <<go>>
    <<makefile>>
    makefile is a file
    <<qs-test1.c>>
    qs-test1.c is a file
    <<qs-test2.c>>
    qs-test2.c is a file
    <<RCS>>
    <<rep-report.txt>>
    rep-report.txt is a file
    <<select.c>>
    select.c is a file
    <<soq>>
    <<xx.pl>>
    xx.pl is a file
    <<yy.pl>>
    yy.pl is a file
    Minimac JL:
    

    Note the Perlish idiom ‘next if -d $item;‘. Also note the debugging techniques: print the names as they go through the array – using the ‘<<‘ and ‘>>’ to surround the name helps identify odd side effects (such as newlines in names). I did double check that the provided code produces the same result – it does. And I’m running on a MacOS X 10.6.3 with the stock Perl.

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

Sidebar

Ask A Question

Stats

  • Questions 400k
  • Answers 400k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Put the connection string in the config file, that way… May 15, 2026 at 4:22 am
  • Editorial Team
    Editorial Team added an answer For classes that extend generic superclasses or implement generic interfaces,… May 15, 2026 at 4:22 am
  • Editorial Team
    Editorial Team added an answer http://www.sqlite.org/lang_insert.html insert or replace might interest you May 15, 2026 at 4:22 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.