how to run perl script from a perl script?

Discussion in 'Perl Misc' started by anywherenotes, Apr 13, 2005.

  1. I have a rather interesting requirement.

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

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

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

    But is there a better way?

    Currently I am doing this:
    require "";

    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.
    anywherenotes, Apr 13, 2005
    1. Advertisements

  2. anywherenotes

    xhoster Guest

    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.

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

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

    Better yet, turn 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 $!;
    xhoster, Apr 13, 2005
    1. Advertisements

  3. anywherenotes

    peter pilsl Guest

    Why not include the code from in 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 directly into 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.

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

    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
    open LOG, ">>mylog.log" or die "can't open log\n";
    system ("perl >>mylog.log");
    close LOG;

    $ cat
    print "";
    $ perl
    The process cannot access the file because it is being used by another
    anywherenotes, Apr 13, 2005
  5. anywherenotes

    peter pilsl Guest

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

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

    peter pilsl, Apr 13, 2005
  6. wrote in
    Fine, if you so wish.

    A. Sinan Unur, Apr 13, 2005

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

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

    my $b_out = `perl 2>&1`;
    open OUT, '>>mylog.log' or die ...
    print OUT $b_out;
    close OUT;
    Tad McClellan, Apr 14, 2005
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.
Similar Threads
There are no similar threads yet.