伙计们,我现在真的很困惑。我是学习Perl的新手。我读的这本书有时会编写Perl代码,有时会执行Linux命令。
它们之间有什么联系吗?(Perl代码和linux命令)
我想使用Perl代码打开多个文件,我知道如何在Perl中使用以下命令打开单个文件:
open (MYFILE,'somefileshere');我还知道如何在Linux中使用ls命令查看多个文件。
那么如何做到这一点呢?我可以在perl中使用ls吗?而且我只想打开某些文件(perl文件),这些文件没有可见的文件扩展名(我想我不能使用*.txt等)。
帮点小忙
发布于 2013-05-10 10:08:26
使用system函数执行linux命令,glob -获取文件列表。
http://perldoc.perl.org/functions/system.html
http://perldoc.perl.org/functions/glob.html
像这样:
my @files = glob("*.h *.m"); # matches all files with a .h or .m extension
system("touch a.txt"); # linux command "touch a.txt"发布于 2013-06-21 06:14:30
目录句柄也非常好用,特别是在遍历目录中的所有文件时。示例:
opendir(my $directory_handle, "/path/to/directory/") or die "Unable to open directory: $!";
while (my $file_name = <$directory_handle>) {
next if $file_name =~ /some_pattern/; # Skip files matching pattern
open (my $file_handle, '>', $file_name) or warn "Could not open file '$file_name': $!";
# Write something to $file_name. See <code>perldoc -f open</code>.
close $file_handle;
}
closedir $directory_handle;https://stackoverflow.com/questions/16474050
复制相似问题