开发者

How can I append the file foo2.txt to foo1.txt?

Is there any method to execute foo2.pl from foo1.pl in Perl, and append the foo2.txt to the foo1.txt then creat开发者_Python百科e foo3.txt? thanks.

i.e.

foo1.pl

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

foo2.pl

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

How to create foo3.txt file based on foo1.pl.

foo3.txt

Hello
World

Something like append foo2.txt to foo1.txt.

As i know, I can open foo1.txt and foo2.txt, then include the lines in foo3.pl.

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

Is there any good method?


Update my test (ActivePerl 5.10.1)

My foo.pl

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

print "world\n";

my hw.pl (foo.pl and hw.pl at the same directory)

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

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

Output

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

hello '.' is not recognized as an internal or external command, operable program or batch file.**

If hw.pl updated {}:

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

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

Now Output. (a little different for the loacation of hello)

D:\learning\perl>hw.pl '.' is not recognized as an internal or external command, operable program or batch file. hello

[Update].

Fixed. see answer,


run this as a script

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

contents of foo1.pl

!#/bin/perl
print "Hello";

contents of foo2.pl

!#/bin/perl
print "World";

or

simply use the cat command if you are running linux to append foo2.txt to foo1.txt.


Just in case you are being literal about execute foo2.pl from foo1.pl in Perl then this is what you can do:

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

qx is another way to run system commands like backticks. Thus perl foo2.pl is run with the output being sent back to your calling perl script.

So here the same using backticks. Also it uses a direct call to script (which is better):

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

And if you are expecting lots of output from the script then its best not to load it all into memory like above two examples. Instead use open like so:

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

And you can wrap this up into one print statement for "hello world" example:

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

/I3az/


Using a shell script (for example, a .bat file on Windows) to run various Perl scripts and combine their output is one way to solve the problem. However, I usually find that Perl itself provides a more powerful and flexible environment than shell scripts. To use Perl in this way, one place to start is by learning about the system and exec commands.

For example:

# 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';


You can use the following code also

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";

The file content is

WELCOME 
WELCOME2 


If you know beforhand that the script you want to execute from inside the other script is also Perl, you should use do EXPR (https://perldoc.perl.org/functions/do.html). This executes the contents of the file EXPR in the context of the running perl process and saves you from starting new cmd.exe and perl.exe instances.

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

wordl.pl:
print "World";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜