📜  珀尔 |具有 CRUD 操作的目录

📅  最后修改于: 2022-05-13 01:55:22.418000             🧑  作者: Mango

珀尔 |具有 CRUD 操作的目录

Perl 是一种通用且跨平台的编程语言,主要用于文本操作,并用于开发许多软件应用程序,如 Web 开发、图形用户界面应用程序等。它比其他编程语言更受欢迎,因为它更快、功能强大并且 Perl 有很多快捷方式有助于编写快速脚本,从而减少编写时间。
目录在编程语言中用于以列表的形式存储值。目录与文件非常相似。就像文件一样,该目录也允许对其执行多项操作。这些操作用于修改现有目录或创建新目录。

可以对目录执行的不同操作包括:

  1. 创建新目录
  2. 打开现有目录
  3. 读取目录的内容
  4. 更改目录路径
  5. 关闭目录
  6. 删除目录
创建目录


使用 mkdir(PATH, MODE) 创建目录。这个函数有助于创建一个新目录,如果用户想检查文件是否已经存在,可以通过-e 函数来完成。 PATH 使用 MODE函数指定的模式设置路径。
例子:

#!/usr/bin/perl  
  
# Path of the directory
my $directory = 'C:\Users\GeeksForGeeks\Folder\Perl';  
  
# Creating a directory in perl  
mkdir($directory) or die "No $directory directory, $!";  
print "Directory created \n"; 

输出:



打开目录


要在 Perl 中打开一个目录,使用一个短函数opendir DIRHANDLE,PATH 被使用。这里的PATH是要打开的目录的路径。
例子 :

#!/usr/bin/perl  
  
my $directory = 'C:\Users\GeeksForGeeks\Folder';  
opendir (DIR, $directory) or die "No directory, $!";  
while ($file = readdir DIR) 
{  
    print "$file\n";  
}  
closedir DIR;

输出:

在标量和列表上下文中读取目录


读取目录是一项常见任务,因为每次都必须读取文件中存储的内容才能运行代码或理解代码。要读取目录,请使用 readdir DIRHANDLE。用户可以通过两种方式读取目录,即在列表上下文和标量上下文中。
在列表上下文中,代码返回目录中的所有其余条目,如果目录中的条目为空,则将在标量上下文中返回未定义的值,在列表上下文中返回空列表。
标量上下文:
例子:

#!/usr/bin/perl  
use strict; 
use warnings; 
use 5.010; 
  
# Path of the directory
my $directory = shift // 'C:\Users\GeeksForGeeks\Folder'; 
  
# Opening the directory
opendir my $dh, $directory or 
die "Could not open '$directory' for reading '$!'\n"; 
  
# Printing content of the directory
while (my $content = readdir $dh) 
{ 
    say $content; 
} 
  
# Closing the directory
closedir $dh; 

输出:

列出上下文:

#!/usr/bin/perl  
use strict;  
use warnings;  
use 5.010;  
  
# Path of the directory
my $directory  = shift // 'C:\Users\GeeksForGeeks\Folder';  
  
# Opening the directory
opendir my $dh, $directory or 
die "Could not open '$directory' for reading '$!'\n";  
  
# Reading content of the file
my @content = readdir $dh;  
  
# Printing content of the directory
foreach my $content (@content) 
{  
    say $content;  
}  
  
# Closing the directory
closedir $dh;

输出:

更改目录


要更改目录,使用 chdir()函数。此函数有助于更改目录并将其发送到新位置。使用脚本调用 chdir()函数时,会更改脚本其余部分的目录。如果在脚本中调用此函数,则不会更改终端上的目录。另一方面,当直接使用新目录路径调用时,更改会同时反映在终端中。
示例 1:在脚本中调用 chdir() 时

#!/usr/bin/perl
  
# Module to return 
# current directory path
use Cwd;
  
# Path of new directory
$directory = "C:/Users";
  
# Printing the path of current directory
# using cwd function
print "The current directory is ";
print(cwd);
print "\n"; 
  
# Changing the directory using chdir function
chdir($directory) or 
      die "Couldn't go inside $directory directory, $!";
  
# Printing the path of changed directory
print "Directory has been changed to $directory\n";
print "The current directory is ";
print(cwd);
print "\n"; 

输出
示例 2:在终端上直接调用 chdir() 时

关闭目录


要关闭一个目录,使用 DIRHANDLE 关闭目录。在这里,DIRHANDLE 是使用 opendir函数打开它的目录的句柄。
例子:

#!/usr/bin/perl
  
# Directory which is to be opened
$dirname = "C:/Users/GeeksForGeeks";
  
# Opening the directory 
# using opendir function
opendir (dir, $dirname) || die "Error $dirname\n";
  
# Printing content of the directory
# using readdir function
while(($filename = readdir(dir))) 
{
    print("$filename\n");
}
  
# Closing the directory using closedir 
closedir(dir);

输出:

删除目录


可以使用 rmdir函数来删除目录。此函数仅在该目录为空时删除 FILENAME 指定的目录,如果成功则返回 true,否则返回 false。
例子:

#!/usr/bin/perl  
$directory = "C:/Users/GeeksForGeeks/Folder/Perl";  
  
# This removes perl directory  
rmdir($directory ) or
      die "Couldn't remove $directory directory, $!";  
        
print "Directory removed \n";  

输出: