how to run perl script from a perl script?

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

  1. Guest

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

  2. 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 "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);
    Xho
     
    , Apr 13, 2005
    #2
    1. Advertisements

  3. peter pilsl Guest

    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
     
    peter pilsl, Apr 13, 2005
    #3
  4. Guest

    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.
    $
     
    , Apr 13, 2005
    #4
  5. peter pilsl Guest


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

    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
     
    peter pilsl, Apr 13, 2005
    #5
  6. wrote in @l41g2000cwc.googlegroups.com:
    Fine, if you so wish.
    Why?

    Sinan
     
    A. Sinan Unur, Apr 13, 2005
    #6

  7. 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...


    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;
     
    Tad McClellan, Apr 14, 2005
    #7
    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.