in reply to selcting all files in a given directory except......

It would help if you said what the error messages are. Error messages are your friends!

Your first problem is a missing '#' on the first line. That might be a slip when pasting your code, otherwise it might be trying to execute it as a Bourne shell script.
Thereafter, you will get strictness errors because you are not pre-declaring all your variables. Try this:
#!/usr/bin/perl use strict; use warnings; chomp(my $dir = <STDIN>); my @files; if (-d $dir){ opendir (my $DIR, $dir); readdir ($DIR); # ignore . readdir ($DIR); # ignore .. @files = readdir($DIR); closedir($DIR); } foreach my $file (@files) { print "$file\n"; }

Replies are listed 'Best First'.
Re^2: selcting all files in a given directory except......
by glide (Pilgrim) on Dec 06, 2007 at 10:06 UTC
    at least for me, the opendir don't return as the first 2 values the '.' and '..'.
    readdir ($DIR); # ignore . readdir ($DIR); # ignore ..
    the solution of moritz it's more robust
    @files = grep {/^(?!\.{1,2}\z)/} readdir($DIR);

      If you want more robust, don't use a regexp to match parent and current directories.

      @files = grep {$_ ne '.' and $_ ne '..'} readdir($D);

      I know one isn't meant to write code that is easy for a monkey to understand, but all the same, I don't think the added complexity of throwing in a couple of more-rarely used regops like \z and (?!...) is worth it in this case.

      This way there is no ambiguity with strange directories like "foo\n.". Also, your original solution may fail if a corrupted filesystem is rebuilt, and the parent and current directories pointers are rewritten elsewhere than positions 1 and 2 in the directory list.

      If you want to go really cross-platform robust, use File::Spec's curdir() and updir() routines.

      • another intruder with the mooring in the heart of the Perl

      Not all directories have . and .. in them, at least in Windows.
Re^2: selcting all files in a given directory except......
by Anonymous Monk on Dec 06, 2007 at 09:30 UTC
    names of the files are not been printed on the terminal when script is excecuted
      try this script ...
      #!/usr/bin/perl use strict; use warnings; my $dir = $ARGV[0] || $ENV{HOME}; my @files; if (-d $dir){ opendir (my $DIR, $dir); @files = grep {/^(?!\.{1,2}\z)/} readdir($DIR); closedir($DIR); } else { die("can't open the dir, $dir\n"); } foreach my $file (@files) { print "$file\n"; }
      save the script as myls.pl, and run the script like this
      $ perl myls.pl /home
      ps. the $ENV{HOME} exist in the windows environment??
      A reply falls below the community's threshold of quality. You may see it by logging in.
      Maybe the opendir is failing. Modify that line to read:
      opendir (my $DIR, $dir) || die "Unable to open $dir: $!";

      then try again.
        this is the code which i hav been excecuting...... still unable to find out........
        #!/usr/bin/perl use strict; use warnings; chomp(my $dir = <STDIN>); my @files; if (-d $dir){ opendir (my $DIR, $dir) || die "Unable to open $dir: $!"; @files = grep { m/^(?!\.{1,2}\z)/ } readdir($DIR); closedir($DIR); } foreach my $file (@files) { print "$file\n"; }
      When you ran the preceding example,
      • What input did you give it?
      • What was the expected output?
      • What platform/perl are you running?
      -- Ian Tegebo
        i had excuted with ./script test test is directory with test1 test2 file names it didnt print any filenames 2)list the names of all the files in a directory, except for "." and ".." 3)cshell/unix

      Did you enter dir name and press Enter key?

        ./script test(directory name) enter i had done tht
      A reply falls below the community's threshold of quality. You may see it by logging in.