Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: glob() and dot files

by Marshall (Canon)
on Apr 13, 2020 at 05:05 UTC ( #11115417=note: print w/replies, xml ) Need Help??


in reply to glob() and dot files

This glob thing can be a problem. A long time ago I got tripped up with the 3 versions of glob that were in use at that time in the ActiveState version of Perl that I was using. I changed my code to use readdir() and that solved the problem.

Nowadays, Perl glob is a lot more uniform and well behaved. This prints all simple files, but skips directories.

my @files = glob ('*.*'); print "",join("\n",@files),"\n"
For what you want, I would consider File::Find.
Consider this code also.
use strict; use warnings; opendir (my $dir, ".") or die "unable to open current directory $_"; my @files; my @directories; foreach my $file (grep{ ($_ ne '.') and ($_ ne '..')} readdir $dir) { if (-f $file ) {push @files, $file;} elsif (-d $file) {push @directories, $file;} else { die "weird beast found! $file"} } print "@files\n"; print "@directories\n";
I think in Unix there can be special things that are not simple files or directories. I would use a file test to see what this name actually means.
Note that if this is not the current directory, you need to spec the full path name for file tests.

Update: File operations like "open file" or "open directory" are "expensive" in terms of performance. I would expect my code to run faster than the OP's code, but I did not benchmark this in any serious way. If the directories are small and this is not done that often, I don't think that will make any difference at all. Also be aware that there is a special variable for repeated file tests, "_". like  elsif (-d _) {do something{ That tests the structure returned by the previous file test operation for a different flag.

Overall, unless there is a performance or other problem (special kinds of files), I see no problem with the OP's code.

Replies are listed 'Best First'.
Re^2: glob() and dot files
by haukex (Archbishop) on Apr 13, 2020 at 08:26 UTC

    This prints all simple files, but skips directories.

    my @files = glob ('*.*');

    No, it prints any file or directory names that have a dot in them. It's a very old DOS convention that files had extensions and directories didn't, but nowadays that's not true anymore. You'd need grep {!-d} glob('*') to exclude directories.

      You are correct.
      my @files = glob('*'); # current directory print "",join("\n",@files), "\n";
      # Note that these file names do not say whether or not they are directories, a file test is needed. I demo'ed this at Re: Getting a list of directories matching a pattern.

      Update: I experimented with Windows 10 command line and found that I could indeed create a directory with a "dot suffix". That surprised me.Having said that, I have never seen such a thing in "real life". By convention, that is just not "the way that this is done". A long time ago, I was forced to use readdir and grep to get file names because of incompatible glob's. For production code, I still use readdir and grep because it will always work. For quick hacks, I am fine with glob().

        a directory with a "dot suffix". That surprised me.Having said that, I have never seen such a thing in "real life". By convention, that is just not "the way that this is done".

        The two places I see it happen the most are when some programs put their version number as part of the directory name (C:\Program Files\FooBar v1.23) or when *NIX tools are ported to Windows.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11115417]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2022-08-13 22:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?