Perl glob简介及实例

530 阅读3分钟

Perl glob

关于Perl glob的介绍

每当我们想在Perl中打印某个特定目录下的文件名时,我们就会使用Perl中一个叫做glob()的函数。它接收的参数是文件所在目录的路径,这些文件的名称将被glob()函数作为输出返回。这个glob()函数既可以打印目录中所有的文件名,也可以打印目录中特定的文件名,这取决于我们在文件所在目录的路径中所指定的参数,这些文件的名称将被glob()函数作为输出返回。

在Perl中声明glob()函数的语法

glob(path_to_the_directory);

其中path_to_the_directory是指向文件所在目录的路径,这些文件的名称将被glob()函数作为输出结果返回。

glob()函数在Perl中的作用

  • 每当我们想在Perl中打印某个特定目录下的文件名时,我们就会使用Perl中一个叫做glob()的函数。
  • glob()函数接收的参数是文件所在目录的路径,这些文件的名称将被glob()函数作为输出返回。
  • glob()函数既可以打印目录中所有的文件名,也可以打印目录中特定的文件名,这取决于我们在文件所在目录的路径中所指定的参数,这些文件的名称将被glob()函数作为输出结果返回。

Perl glob的例子

下面给出的是Perl glob的例子。

例子 #1

Perl程序通过使用glob()函数来打印指定目录下的文件名。

代码

#making use of glob function by passing the path of the directory where the files are present to print the names of the files present in the directory
@filenames = glob('C:/Users/admin/Desktop/*');
print "The files present in the specified directory are:\n";
foreach $eachfile(@filenames){
print "$eachfile\n";
}

输出

Perl glob 1

在上面的程序中,我们利用glob()函数,通过传递文件所在目录的路径来打印该目录中的文件名。然后,目录中的文件名被打印成屏幕上的输出。

例子#2

Perl程序通过使用glob()函数来打印指定目录下的文件名。

代码

#making use of glob function by passing the path of the directory where the files are present to print the names of the files present in the directory
@filenames = glob('C:/Users/admin/Desktop/EDUCBA/*’);
print "The files present in the specified directory are:\n";
foreach $eachfile(@filenames){
print "$eachfile\n";
}

输出

Perl glob 2

在上面的程序中,我们利用glob()函数,通过传递文件所在目录的路径来打印该目录中的文件名。然后,目录中的文件名被打印成屏幕上的输出。

例子#3

Perl程序通过使用glob()函数来打印指定目录下的文件名。

代码

#making use of glob function by passing the path of the directory where the files are present to print the names of the files present in the directory
@filenames = glob(E:/Images/*’);
print "The files present in the specified directory are:\n";
foreach $eachfile(@filenames){
print "$eachfile\n";
}

输出

files present in the directory s

在上面的程序中,我们利用glob()函数,通过传递文件所在目录的路径来打印该目录中的文件名。然后,目录中的文件名被打印成屏幕上的输出。

例子 #4

Perl程序通过使用glob()函数来打印指定目录下的文件名。

代码

#making use of glob function by passing the path of the directory where the files are present to print the names of the files present in the directory
@filenames = glob(F:/Music/*’);
print "The files present in the specified directory are:\n";
foreach $eachfile(@filenames){
print "$eachfile\n";
}

输出

names of the files present in the directory

在上面的程序中,我们利用glob()函数,通过传递文件所在目录的路径来打印该目录中的文件名。然后,目录中的文件名被打印成屏幕上的输出。

例子#5

Perl程序通过使用glob()函数来打印指定目录下的文件名。

代码

#making use of glob function by passing the path of the directory where the files are present to print the names of the files present in the directory
@filenames = glob(D:/WindowsUpdate/*’);
print "The files present in the specified directory are:\n";
foreach $eachfile(@filenames){
print "$eachfile\n";
}

输出

perl glob 5

在上面的程序中,我们利用glob()函数,通过传递文件所在目录的路径来打印该目录中的文件名。然后,目录中的文件名被打印成屏幕上的输出。