简体   繁体   English

如何从Perl中的其他目录打开文件?

[英]How to open a file from different directory in perl?

i am very new to perl, so I would like to know if there is a way to 我是perl的新手,所以我想知道是否有办法

  1. Open a file from a different directory (not the same directory as the perl script.pl for example) 从其他目录(例如,与perl script.pl不同的目录)中打开文件

  2. open multiple files that have the same name, eg sameName.txt, under the same parent directory but have different sub directory, eg 在同一父目录下打开多个具有相同名称(例如sameName.txt)但具有不同子目录(例如)的文件

     directory: - /alias/a/1/sameName.txt - /alias/b/1/sameName.txt - /alias/c/1/sameName.txt 

    for example as above, but at the same time, there is also the same file, sameName.txt in other directory which I don't want, eg 例如上面的例子,但是同时,我不想在其他目录中也有相同的文件sameName.txt,例如

     directory: - /alias/a/2/sameName.txt - /alias/b/2/sameName.txt - /alias/c/2/sameName.txt 

    How can I automatically search the directory which the user want, using user input <STDIN> , not hard-coded into the script perl.pl for example, the user want all the sameName.txt files which had in the directory /1/sameName.txt, but with different parent, which is ab and c folder. 如何使用用户输入<STDIN>自动搜索用户想要的目录,例如,未将其硬编码到脚本perl.pl中,用户想要目录/ 1 / sameName中具有的所有sameName.txt文件.txt,但具有不同的父文件夹,即ab和c文件夹。 I want to make it automatically read those files sameName.txt which is located in different folder, so that the user doesn't need to adjust the script every time there is a new path like d/1/sameName.txt created. 我想让它自动读取位于不同文件夹中的那些文件sameName.txt,以便用户不必每次创建新路径(例如d / 1 / sameName.txt)时都对脚本进行调整。

  3. if I want the data in these files with the same name with different directories, should I loop it, save into arrays for example, or should i copy all the contents and append it to a single file? 如果我希望这些文件中的数据具有相同的名称,但位于不同的目录中,我应该将其循环,例如保存到数组中,还是应该复制所有内容并将其附加到单个文件中? because i need to match the data between the files which i have done the script. 因为我需要匹配完成脚本的文件之间的数据。

  1. You can open a file from anywhere that you like. 您可以从任意位置打开文件。

The filename argument is a path and you associate that with a filehandle to access its data: filename参数是一个路径,您可以将其与文件句柄关联以访问其数据:

  my $path = '/alias/a/1/sameName.txt';
  open my $fh, '<', $path or die "Could not open $path: $!";
  1. Perl doesn't care if another file in a different directory has the same name. Perl不在乎其他目录中的另一个文件是否具有相同的名称。

You distinguish them with a different file handle: 您使用不同的文件句柄来区分它们:

  my $path2 = '/alias/a/2/sameName.txt';
  open my $fh2, '<', $path or die "Could not open $path: $!";

You can construct that second path by grabbing the filename portion of the first path and putting it together with the other directory. 您可以通过抓住第一个路径的文件名部分并将其与其他目录放在一起来构造第二个路径。 These are core Perl modules that should already be there: 这些是应该已经存在的核心Perl模块:

  use File::Basename;
  use File::Spec::Functions;

  my $other_dir = '/alias/a/2';
  my $basename = basename( $path );   # sameName.txt
  my $path2 = catfile( $other_dir, $basename );
  1. Not quite sure what you are trying to do. 不太确定您要做什么。

You might be interested in Learning Perl or the other resources at learn.perl.org . 您可能对Learning Perllearning.perl.org上的其他资源感兴趣

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM