【问题标题】:How can I append the file foo2.txt to foo1.txt?如何将文件 foo2.txt 附加到 foo1.txt?
【发布时间】:2018-02-12 22:47:08
【问题描述】:

有什么方法可以在 Perl 中从 foo1.pl 执行 foo2.pl,并将 foo2.txt 附加到 foo1.txt 然后创建 foo3.txt?谢谢。

foo1.pl

 print "Hello"; # output to foo1.txt;

foo2.pl

 print "World"; # output to foo2.txt;

如何基于foo1.pl创建foo3.txt文件。

foo3.txt

Hello
World

类似于将 foo2.txt 附加到 foo1.txt

据我所知,我可以打开 foo1.txtfoo2.txt,然后将这些行包含在 foo3.pl 中。 p>

print FOO3_TXT (<FOO1_TXT>);
print FOO3_TXT (<FOO2_TXT>);

有什么好的方法吗?


更新我的测试 (ActivePerl 5.10.1)

我的 foo.pl

#!C:\Perl\bin\perl.exe
use strict;
use warnings;

print "world\n";

我的hw.pl(foo.pl和hw.pl在同一目录)

#!C:\Perl\bin\perl.exe
use strict;
use warnings;

print 'hello '; 
print `./foo.pl`;

输出

**D:\learning\perl>hw.pl

你好'。'不被识别为内部或外部命令, 可运行的程序或批处理文件。**

如果 hw.pl 更新 {}:

#!C:\Perl\bin\perl.exe
use strict;
use warnings;

print q{hello }, qx{./foo.pl};

现在输出。 (hello的位置略有不同)

D:\learning\perl>hw.pl '。'不被识别为内部或外部命令, 可运行的程序或批处理文件。 你好

[更新]。

已修复。看答案,

【问题讨论】:

  • 回复:您的更新 - 请参阅我在回答中的最后一条评论。

标签: perl append


【解决方案1】:

将其作为脚本运行

perl foo1.pl > foo3.txt;
perl foo2.pl >> foo3.txt;

foo1.pl 的内容

!#/bin/perl
print "Hello";

foo2.pl 的内容

!#/bin/perl
print "World";

如果您正在运行 linux,只需使用 cat 命令将 foo2.txt 附加到 foo1.txt。

【讨论】:

  • 嗨iamrohitbanga,我用windows命令行一一测试。它运作良好。但我不知道如何编写脚本。我可以将 scipt 创建为 .pl 文件吗?谢谢。
  • 我创建了一个 try.bat 包含 perl foo1.pl &gt; foo3.txt; perl foo2.pl &gt;&gt; foo3.txt; 它可以工作。谢谢。
  • 如果它满足您的需要,请将其标记为答案。我以前没有回答过很多问题。
  • 嗨,iamrohitbanga,也谢谢你。
【解决方案2】:

以防万一您对execute foo2.pl from foo1.pl in Perl 直言不讳,那么您可以这样做:

print 'hello ';
print qx(perl foo2.pl);

qx 是运行反引号等系统命令的另一种方式。因此,perl foo2.pl 运行时将输出发送回调用 perl 脚本。

所以这里同样使用反引号。它还使用直接调用脚本(更好):

print 'hello ';
print `./foo2.pl`;

如果您希望脚本有大量输出,那么最好不要像上面两个示例那样将其全部加载到内存中。而是像这样使用open

print 'hello ';
open my $foo2, '-|', './foo2.pl';
print <$foo2>;
close $foo2;

您可以将其包装成“hello world”示例的打印语句:

print 'hello ', do {
   open my $foo2, '-|', './foo2.pl';
   <$foo2>;
};

/I3az/

【讨论】:

  • 您好 draegtun,我运行您的脚本并显示错误消息:“hello '。”无法识别为内部或外部命令、可运行程序或批处理文件。请告诉我为什么?
  • 顺便说一句,我认为 print 'hello ';打印./foo2.pl就是我想要的!
  • 嗨 Nano HE,这个错误听起来好像有些东西没有正确复制/粘贴,也许它把那些单引号变成了反引号?当我在这里复制我的第一个示例时,一切正常。所以试试这个 Perl sn-p:print q{hello }, qx{./foo2.pl};
  • 嗨。仍然无法工作。我不知道为什么?我在帖子中更新了我的详细测试。
  • Windows 嗯! ./foo2.pl 是 *nix 特有的,所以从外观上看,它不适用于准系统 Windows。将其更改为正确的 Windows 路径,它将正常工作。所以对于一个可执行脚本来说:print q{hello }, qx{C:\path\foo2.pl}; 现在应该可以工作了!
【解决方案3】:

使用 shell 脚本(例如,Windows 上的 .bat 文件)运行各种 Perl 脚本并组合它们的输出是解决问题的一种方法。但是,我通常发现 Perl 本身提供了比 shell 脚本更强大和更灵活的环境。要以这种方式使用 Perl,首先要了解 systemexec 命令。

例如:

# In hello.pl
print "Hello\n";

# In world.pl
print "World\n";

# In hello_world.pl.
use strict;
use warnings;
system 'perl hello.pl >  hello_world.txt';
system 'perl world.pl >> hello_world.txt';

【讨论】:

    【解决方案4】:

    你也可以使用下面的代码

    file1.pl

    use strict;
    use warnings;
    
    open (FH,">file") or die "$! can't open";
    print FH "WELCOME\n";
    

    file2.pl

    use strict;
    use warnings;
    
    open (FH,">>file") or die "$! can't open";
    print FH "WELCOME2\n";
    

    文件内容为

    WELCOME 
    WELCOME2 
    

    【讨论】:

      【解决方案5】:

      如果您事先知道要从另一个脚本中执行的脚本也是 Perl,您应该使用 do EXPR (https://perldoc.perl.org/functions/do.html)。 这会在正在运行的 perl 进程的上下文中执行文件 EXPR 的内容,并使您免于启动新的 cmd.exe 和 perl.exe 实例。

      hello.pl:
      print "Hello";
      do "world.pl";
      
      wordl.pl:
      print "World";
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-03-30
        • 2013-01-20
        • 2012-06-03
        • 1970-01-01
        • 2020-02-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多