Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

execute a perl program

by myraleen (Initiate)
on Dec 12, 2003 at 15:06 UTC ( #314322=perlquestion: print w/replies, xml ) Need Help??

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

You'll all have to excuse me, but i'm brand spankin new to perl, and completely lost. I'm still in PHP mode I think in my thinking.

I want to run a perl script from the cmd line.

perl perl_run.pl

So that's all fine and dandy.

But...
Within perl_run.pl I want it to 'call' another perl script, and run it.

How do i go about it?
I"ve been reading, and found this OPEN INFILE OUTFILE... but it totally confuses me.

And it's not working. Can someone help?

~*~myraleen~*~ ~damn squigglies~

Edit: Chady -- fixed formatting.

Replies are listed 'Best First'.
Re: execute a perl program
by tcf22 (Priest) on Dec 12, 2003 at 15:17 UTC
    You could run it as a seperate process
    system("perl other.pl");
    or
    $out = `perl other.pl`;
    or you could just require the script and it will execute it. I would go with this option.
    require 'other.pl';

    - Tom

Re: execute a perl program
by calin (Deacon) on Dec 12, 2003 at 16:05 UTC
    By growing level of sophistication:

    exec => system

    do => require => use

    The first set: these calls either replace the current process (exec) or spawn a child process and wait for it (system). The called program can be any executable, they're not limited to Perl. In fact, they're wrappers for the system calls fork and exec, with some added glue.

    The second set: these calls are used to import Perl code contained in an external file into the running Perl process.

    Take a look at the docs to see what which one does and how the more sophisticated calls can be explained in terms of the lesser ones. Any one of them can be useful in a given situation (sometimes use is not what you want!).

    There are more options, especially if you want to communicate with the child process by STDIN/STDOUT/STDERR: open (see also perlopentut), qx// and backticks (in perlop) and the IPC family of modules (IPC::Open2 and IPC::Open3 are useful) etc.

      IN addition to the IPC family of modules, check out the Expect module.
Re: execute a perl program
by Arbogast (Monk) on Dec 12, 2003 at 15:48 UTC
    Learn the system, exec and `` (backticks) commands to run a program from a Perl script.

    If you are on Windows, also learn the Win32::Process module.

    Win32::Process

    Then you will be in business.
Re: execute a perl program
by punkish (Priest) on Dec 14, 2003 at 00:49 UTC
    one thing to keep in mind (get used to) as you migrate from PHP to Perl mode of thinking -- you can't really include/execute files the way you could in the PHP world. You can use "use" or "require" but the script that you have called via use or require has to compile properly. That means, it doesn't depend on any variable that hasn't been created yet, if it creates any variables that you want to access in the calling script then those vars have to be exported, and if the script has only subroutines in it then you really need a line like
    1;
    at the very end to let the Perl compiler know that every thing was ok. Otherwise, it will compile everything and won't have anything to show for it, and will complain.

    Of course, if you really want to run some external file and use its results, why not just consider typing it in the same file... but, since that is a matter of preference, I leave that for you to decide.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://314322]
Approved by Chady
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2021-12-06 14:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (32 votes). Check out past polls.

    Notices?