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

  2. Guest

    wrote:
    > 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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Apr 13, 2005
    #2
    1. Advertising

  3. peter pilsl Guest

    wrote:
    >
    > 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





    --
    http://www.goldfisch.at/know_list
     
    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

    wrote:
    > 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

    --
    http://www.goldfisch.at/know_list
     
    peter pilsl, Apr 13, 2005
    #5
  6. wrote in news:1113426518.584156.270090
    @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

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Apr 13, 2005
    #6
  7. <> wrote:

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


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 14, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wet Basement
    Replies:
    1
    Views:
    2,590
    BobMonk
    Jul 15, 2003
  2. dpackwood
    Replies:
    3
    Views:
    1,863
  3. Replies:
    9
    Views:
    1,026
    Paddy O'Loughlin
    Feb 27, 2009
  4. pj
    Replies:
    3
    Views:
    321
  5. hara
    Replies:
    6
    Views:
    177
    Peter Scott
    May 27, 2006
Loading...

Share This Page