The_Last_Monk has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Shaolin monks, So i am trying to print the names of a file in a directory, here is my code:
$dir = path to the directory opendir (DIR,$dir) or die "dead"; while(my $text = readdir DIR) { print "$file\n" }
The directory has a text file in it, but once it opens the directory, the output is this:
. .. File.txt
What would be a solution to get rid of those dots? Is there a regex that should be used here so that it only prints the files starting with a word or a number? If so, what would be the regex?

Replies are listed 'Best First'.
Re: Dots appearing when opening a directory
by LanX (Saint) on Mar 06, 2015 at 16:54 UTC
    maybe
    next if $text =~ /^\.+$/;

    (untested)

    NB: this will also exclude files consisting of dots only.

    otherwise

    next if $text =~ /^\.\.?$/;

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)

    PS: Je suis Charlie!

Re: Dots appearing when opening a directory
by toolic (Bishop) on Mar 06, 2015 at 17:08 UTC
    What would be a solution to get rid of those dots?
    File::Slurp read_dir removes them by default:
    use warnings; use strict; use File::Slurp qw(read_dir); for my $file (read_dir('.')) { print "$file\n"; }
      Unfortunately, the perl version we need to use is about 8 years old, and doesn't have the File library in it, so can't use tht :(
Re: Dots appearing when opening a directory
by Anonymous Monk on Mar 06, 2015 at 17:08 UTC

    . is "the current directory" and .. is "the parent directory". File::Spec provides the functions no_upwards, curdir and updir to help deal with them.

    #!/usr/bin/env perl use warnings; use strict; use File::Spec::Functions qw/no_upwards curdir updir/; my $DIR = "/tmp/foo"; opendir my $dh1, $DIR or die $!; while(my $text = readdir $dh1) { next if $text eq updir || $text eq curdir; print "1: $text\n"; } closedir $dh1; opendir my $dh2, $DIR or die $!; my @files = no_upwards readdir $dh2; closedir $dh2; print "2: $_\n" for @files; __END__ 1: File.txt 2: File.txt
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Dots appearing when opening a directory
by karlgoethebier (Abbot) on Mar 06, 2015 at 17:55 UTC

    I'm still endorsing IO::All:

    #!/usr/bin/env perl use strict; use warnings; use IO::All; use feature qw(say); say for io('myDir')->all; __END__ karls-mac-mini:monks karl$ mkdir myDir karls-mac-mini:monks karl$ touch myDir/File.txt karls-mac-mini:monks karl$ ls -al myDir/ total 0 drwxr-xr-x 3 karl karl 102 6 Mär 18:38 . drwxr-xr-x 410 karl karl 13940 6 Mär 18:44 .. -rw-r--r-- 1 karl karl 0 6 Mär 18:38 File.txt karls-mac-mini:monks karl$ ./1119062.pl myDir/File.txt

    From the docs:

    all (For directories) Returns a list of IO::All objects for all files and subdirectories in +a directory. '.' and '..' are excluded.

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Re: Dots appearing when opening a directory
by trippledubs (Deacon) on Mar 06, 2015 at 18:11 UTC
    You can use glob, which expands like a shell would and excludes all files/directories that start with .
    for (glob('*')) { print "$_\n"; }
    You can also use grep with regex match operating on readdir.
    opendir(my $dh, '.') or die $!; for ( grep { !/^\.+$/ } readdir $dh ){ print "$_\n"; }
    Change the regex to  grep { /^[\w\d]/ } if you want only things that begin with a word or digit.

      On a Windows system, glob gave me huge fits of incorrect results in early versions of ActiveState Perl, leading me to write my filenames.pm. When CPAN modules came out, they got increasingly closer to the goal, but still kept missing the mark.

      In those days, I was unable to devote time to working on taking my lessons learned in the writing of my filenames.pm module and applying them to the various CPAN modules (a recurring theme for me during that era of my career, unfortunately), so I grew into habitual use of my own module.

      In retrospect, I wish I'd gotten more involved in CPAN modules instead of rolling my own all the time; I'd have a much better toolkit at my disposal now, and as an added benefit I'd probably be better and faster at installing CPAN modules on the rare occasion where I find one could save me some development time.

      Ah, the heady days of youth.

Re: Dots appearing when opening a directory
by Laurent_R (Canon) on Mar 06, 2015 at 19:03 UTC
    The glob function is probably the easiest solution, and it is necessarily on your Perl environment.

    Otherwise you may filter out directories and keep only files:

    opendir my $dh, $dirname or die "could not open $dirname $!"; print "$_\n" for grep { -f $_ } readdir $dh ;

    Je suis Charlie.
Re: Dots appearing when opening a directory
by Anonymous Monk on Mar 06, 2015 at 17:14 UTC