Anish has asked for the wisdom of the Perl Monks concerning the following question:

I want to do the following temp1.pl looks like this if (condition){ statement }else{ temp2.pl } </code> both files are in same directory. how can I invoke temp2.pl in temp1.I want the syntax. Thanx.

Replies are listed 'Best First'.
Re: Invoking perl script
by Adam (Vicar) on Oct 17, 2000 at 22:54 UTC
    do -- Uses the value of EXPR as a filename and executes the contents of the file as a Perl script.

    if (condition) {blah} else { do "script2.pl" }

RE: Invoking perl script
by Zarathustra (Beadle) on Oct 18, 2000 at 07:51 UTC

    As well as do, there are:

    exec()

    and

    system()

    use system('/path/to/temp2.pl'); if you want to block while temp2.pl executes, and then proceed with the rest of temp1.pl

    use exec('/path/to/temp2.pl'); if you want temp2.pl to execute and then terminate temp1.pl directly afterward, essentially short-circuiting the rest of the code in temp1.pl

      This may be more understandable as explaining system() as a secure use of a fork() and an exec() and exec() as a function that will load a program directly over your current program (actually replaces your current executable with a new one). Not a criticism, just a note. FYI.

      Please note that I never stated that system() is just a call to fork() and exec(), it is much more complicated than that. Implementing it yourself with fork() and exec() requires between 50-80 lines of C to be properly POSIX.2-compliant (involves proper shell env handling and signal handling).

      AgentM Systems nor Nasca Enterprises nor Bone::Easy is responsible for the comments made by AgentM.</h6>