Perl Tutorial
Fundamentals
Input and Output
Control Flow
Arrays and Lists
Hash
Scalars
Strings
Object Oriented Programming in Perl
Subroutines
Regular Expressions
File Handling
Context Sensitivity
CGI Programming
Misc
In Perl, you can access directory contents using either the opendir
and readdir
functions or the glob operator (<>
). The glob operator is sometimes referred to as "file globbing." It's a way to get file and directory names using wildcards.
Here's a tutorial on how to access a directory using file globbing in Perl:
The glob operator returns a list of filenames and directory names in a directory.
Example 1: List all files and directories in the current directory:
#!/usr/bin/perl use strict; use warnings; my @files = <*.*>; foreach my $file (@files) { print "$file\n"; }
Example 2: List all .txt
files in the current directory:
#!/usr/bin/perl use strict; use warnings; my @txt_files = <*.txt>; foreach my $file (@txt_files) { print "$file\n"; }
<*.txt *.md>
.opendir
and readdir
may be more efficient.While file globbing is handy, if you need more control or want to perform more complex operations when accessing directories (like filtering based on more intricate conditions, recursively accessing directories, etc.), you should consider using the opendir
and readdir
functions or the File::Find
module.
Always be cautious when dealing with file and directory operations, especially if you're performing delete or modify operations, to avoid unintended data loss.
In conclusion, file globbing in Perl provides a quick and readable way to access directory contents based on specific patterns. It's suitable for many routine tasks and scripting needs.
Using glob()
function to access directory contents in Perl:
glob()
function in Perl is used to expand filenames using wildcards or globbing patterns.# Using glob() to list all files in the current directory my @files = glob('*'); print "Files in the current directory: @files\n";
File globbing patterns in Perl:
*
, ?
, etc.) to match filenames based on specific patterns.# Using wildcard pattern to list all text files my @text_files = glob('*.txt'); print "Text files in the current directory: @text_files\n";
Iterating through files in a directory with Perl globbing:
glob()
and a loop.# Iterating through files in the current directory foreach my $file (glob('*')) { print "File: $file\n"; }
Recursive file globbing in Perl:
File::Find
module for recursive file globbing to traverse directories.use File::Find; my @files; find(sub { push @files, $File::Find::name if -f }, '.'); print "All files recursively: @files\n";
Filtering files based on patterns with Perl glob:
glob()
with specific wildcards.# Filtering files based on pattern my @filtered_files = glob('*.{txt,pl}'); print "Files with .txt or .pl extension: @filtered_files\n";
Accessing specific file types in Perl using globbing:
# Accessing only Perl script files my @perl_scripts = glob('*.pl'); print "Perl script files: @perl_scripts\n";
Directory traversal using Perl file globbing:
glob()
with a recursive pattern.# Directory traversal using glob() my @all_files = glob('*/**/*'); print "All files in subdirectories: @all_files\n";
Excluding files and directories with Perl globbing:
glob()
with negative patterns.# Excluding files with a specific extension my @non_txt_files = glob('*.*[!txt]'); print "Files excluding .txt extension: @non_txt_files\n";
Sorting files obtained through Perl globbing:
glob()
for better organization.# Sorting files obtained through glob() my @sorted_files = sort glob('*'); print "Sorted files in the current directory: @sorted_files\n";