how to run perl script from a perl script?

A

anywherenotes

I have a rather interesting requirement.

I have 2 perl scripts, and one of them should execute the other one.
So lets say script a.pl should execute script b.pl.

The output of the script b.pl should go into a specific file which only
a.pl knows about.

So I could do this from a.pl:
system("perl b.pl >>mylog.log 2>&1");

But is there a better way?

Currently I am doing this:
require "b.pl";

But it doesn't redirect the output as I want it to.

I am a perl noobie, so I am probably overlooking the correct way of
doing this.
 
X

xhoster

I have a rather interesting requirement.

I have 2 perl scripts, and one of them should execute the other one.
So lets say script a.pl should execute script b.pl.

The output of the script b.pl should go into a specific file which only
a.pl knows about.

I think "interesting" is the very polite way of describing your
requirement. It would probably make more sense to refine your requirement,
rather than refine your method of achieving the that requirement.

So I could do this from a.pl:
system("perl b.pl >>mylog.log 2>&1");

But is there a better way?

What do you mean by better? Faster? Less memory? Easier to maintain?
Fewer keystrokes? More secure? More intuitive? More portable?

Currently I am doing this:
require "b.pl";

But it doesn't redirect the output as I want it to.

redirect STDOUT (and it looks like STDERR) before the 'require "b.pl";'

Better yet, turn b.pl into a module (and preferably rename it) which
defines a subroutine that takes a file handle as an argument and prints its
output to that filehandle.

Then just do:

use b;
open my $fh, ">whatever" or die $!;
b::run($fh);
I am a perl noobie, so I am probably overlooking the correct way of
doing this.

Xho
 
P

peter pilsl

I have 2 perl scripts, and one of them should execute the other one.
So lets say script a.pl should execute script b.pl.

The output of the script b.pl should go into a specific file which only
a.pl knows about.

Why not include the code from b.pl in a.pl. Directly or via a module?

If this is not possible or you need only a quick fix, then take a look
at pipes. You can pipe the output from b.pl directly into a.pl without
needing a temporary file, that - according to your requirements - is a
potential securityrisk when read by someone else.

And of course you can redirect STDOUT as Xho stated, but I'm not sure if
this is the best approach to your problem.


best,
peter
 
A

anywherenotes

Thank you for both replies. I don't want to put code from b.pl to
a.pl (not actual names, they do have normal names) because it's likely
someone would want to run "b.pl" independently of a.pl.
Basically a.pl checks for lots of things wrong with the system. b.pl
checks for only a couple of things, so a.pl calls b.pl to do some
checking.

I am currently using pipes, as I found that I couldn't use system the
way I described. Something that I don't know how to do on Windows I
guess ... or I'll just call it a windows bug :) - it works fine on Unix
(see below in case you can solve it).

$ cat a.pl
open LOG, ">>mylog.log" or die "can't open log\n";
system ("perl b.pl >>mylog.log");
close LOG;

$ cat b.pl
print "b.pl";
$ perl a.pl
The process cannot access the file because it is being used by another
process.
$
 
P

peter pilsl

Thank you for both replies. I don't want to put code from b.pl to
a.pl (not actual names, they do have normal names) because it's likely
someone would want to run "b.pl" independently of a.pl.


Well. then put b.pl in a module and let b.pl just we a wrapper to this
module and also load the module in a.pl. That would make sense and be
elegant ;)

I am currently using pipes, as I found that I couldn't use system the
way I described. Something that I don't know how to do on Windows I
guess ... or I'll just call it a windows bug :) - it works fine on Unix
(see below in case you can solve it).

$ cat a.pl
open LOG, ">>mylog.log" or die "can't open log\n";
system ("perl b.pl >>mylog.log");
close LOG;

$ cat b.pl
print "b.pl";
$ perl a.pl
The process cannot access the file because it is being used by another
process.
$

What you are doing has nothing to do with pipes.
And the error-message is quite clear. I never programm windows, but it
seems that it cant write-open a file for two processes at the same time.
In your case this does not seem to be necessary after all. Close the
handle before calling the other script and then reopen.

Or share the filehandles, which will make things much more complicated ;)


best,
peter
 
A

A. Sinan Unur

(e-mail address removed) wrote in @l41g2000cwc.googlegroups.com:
guess ... or I'll just call it a windows bug :)

Fine, if you so wish.
$ cat a.pl
open LOG, ">>mylog.log" or die "can't open log\n";
system ("perl b.pl >>mylog.log");
close LOG;

Why?

Sinan
 
T

Tad McClellan

I have 2 perl scripts, and one of them should execute the other one.
So lets say script a.pl should execute script b.pl.

The output of the script b.pl should go into a specific file which only
a.pl knows about.


If only there was a way to capture the output of b.pl into a.pl
so that it could write it to the the correct place...

So I could do this from a.pl:
system("perl b.pl >>mylog.log 2>&1");

But is there a better way?


Yes, and the documentation for the function you are using tells about it.


my $b_out = `perl b.pl 2>&1`;
open OUT, '>>mylog.log' or die ...
print OUT $b_out;
close OUT;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top