tags:

views:

196

answers:

4

Hi. Is there any method to execute foo2.pl from foo1.pl in Perl, and append the foo2.txt to the foo1.txt then create 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,

+2  A: 

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.

iamrohitbanga
Hi iamrohitbanga, I tested with windows command line one by one. It works well. But i don't know how to write the script. Could i create the scipt as a **.pl** file? thank you.
Nano HE
I created a **try.bat** include `perl foo1.pl > foo3.txt; perl foo2.pl >> foo3.txt;` It works. thank you.
Nano HE
if it satisfies your need mark it as an answer. i haven't answered many questions before.
iamrohitbanga
Hi iamrohitbanga, Thank you too.
Nano HE
+1  A: 

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/

draegtun
Hi draegtun, I run your script and show the error message: "hello '.' is not recognized as an internal or external command, operable program or batch file.: Could you please tell me why?
Nano HE
Btw, I think <code>print 'hello '; print `./foo2.pl`; </code> is what i want!
Nano HE
Hi Nano HE, The error sounds like something hasn't copy/pasted over properly and perhaps its mangling those single quote into backticks? When I copy my first example here it all works fine. So try this Perl snippet: `print q{hello }, qx{./foo2.pl};`
draegtun
Hi. Still can't work. I don't know why? I updated my detail testing at my post.
Nano HE
Windows eh! The `./foo2.pl` is *nix specific so doesn't work on barebones Windows by the looks of it. Change it to a proper Windows path and it will work fine. So for an executable script then: `print q{hello }, qx{C:\path\foo2.pl};` should hopefully work now!
draegtun
GREAT~! It works now. Thank you very much.
Nano HE
+1  A: 

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';
FM
It's cool. It works!
Nano HE
+1  A: 

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 
muruga