1. Perl
  2. builtin functions
  3. here

glob function - get list of files contained in directory

You can use the glob function to get a list of files.

my @all_files = glob "*";

Use the glob operator to get a list of files. The target is files (including directories) in the current directory. When you get the file list of another directory, move the current directory with chdir function, or express it with a relative path or an absolute path. ..

Example of glob argument expression

"*" All files that do not start with.
"*.TXT" Files ending in .txt
"* .txt * .pl .txt or a file ending in .pl (there is a space between .txt and .pl)
"*. *" All files (with spaces between * and. *)
"lib/* Files under the lib directory
c:/dir/* Specified by absolute path

You can specify multiple patterns by separating them with a blank. In addition to relative paths, you can also use absolute paths. * Is one of the wildcards used in the Unix shell. The glob function expands filenames containing * according to shell wildcard expansion rules.

Example

This is an example glob function.

use strict;
use warnings;

# Get the list of files in the current directory.
# glob

print "1: Get the list of files in the current directory.\n";
# * Represents all files that do not start with.
my @all_files = glob "*";
print join("\n", @all_files). "\n\n";

print "2: Get a list of files ending in .txt.\n";
my @text_files = glob "* .txt";
print join("\n", @text_files). "\n\n";

print "3: Get a list of files ending in .txt and .pl.\n";
# If there are multiple patterns, separate them with a blank
my @text_and_pl_files = glob "* .txt * .pl";
print join("\n", @text_and_pl_files). "\n\n";

print "4: Get a list of regular files.\n";
my @normal_files = glob "*";
# If you want to do complicated things, use "*"
# Get all the files and then
# Sort by grep.
@normal_files = grep {-f $_} @normal_files;
print join("\n", @normal_files). "\n\n";

Reference: join function

Output

1: Get the list of files in the current directory.
a.exe
a.pl
aaa
DBIx-Custom
dir_20080530_4944
hello.exe
hello.pl
lib
yuki-kimoto-Portablebbs-b578c1d
yuki-kimoto-Portablebbs-b578c1d.zip

2: Get a list of files ending in .txt.


3: Get a list of files ending in .txt and .pl.
a.pl
hello.pl

4: Get a list of regular files.
a.exe
a.pl
hello.exe
hello.pl
yuki-kimoto-Portablebbs-b578c1d.zip

Use the directory handle because the glob function doesn't work properly on Windows

I think you often use the glob function to get a list of files. But for now, it's best to see that the glob function doesn't work properly on Windows. If the directory name contains a specific string, the file list cannot be obtained correctly.

So, instead of using the glob function, it's a good idea to create a function that gets a list of filenames like this: opendir function to open the directory handle, readdir function to read the directory Use>.

sub get_files {
  my $dir = shift;
  
  opendir my $dh, $dir
    or die qq/Can't open directory "$dir":$!/;
  
  my @dirs = map {"$dir/$_"} grep {$_ ne '.' && $_ ne ' ..'} readdir $dh;
  
  return @dirs;
}

How to use

my @files = get_files ('c:/pictures');

Related Informatrion