Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Launching an .exe file from Perl

by Anonymous Monk
on Jul 07, 2000 at 21:29 UTC ( [id://21535]=perlquestion: print w/replies, xml ) Need Help??

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

Is there a way that I can launch an .exe file from Perl? I'm hoping to use this to create a perl program that launches several files. That way when I click on an icon it launches all my graphics programs. Thanks a lot.

Replies are listed 'Best First'.
Re: Launching an .exe file from Perl
by le (Friar) on Jul 07, 2000 at 21:34 UTC
    There are several ways to execute other programs from within Perl, but as I assume you're running Windows (*.exe), you should use either system() or backticks. System just runs the program, backticks collect the output from the executed program.
    system("netscape.exe") && die "Couldn't start netscape: $!\n"; $dir = `dir`;
    There are other ways, like forking, but I think this is not possible on Win32 systems.
Re: Launching an .exe file from Perl
by c-era (Curate) on Jul 07, 2000 at 21:36 UTC
    There are several ways, here are a few:
    # To just run a program use system system ("c:\\my.exe\n"); # To get the output of a program use back tics (use ` not ') $output =`c:\my.exe`; # You can also use open with a pipe open (PROGRAM,"|c:\\my.exe"); #to write to the program open (PROGRAM,"c:\\my.exe|"); #to read from the program open (PROGRAM,"|c:\\my.exe|"): # DOES NOT WORK!!!!! # if you need to read and write from a program you will need to use ip +c; open2, expect, Comm.pl can do that for you
Re: Launching an .exe file from Perl
by Shoeboy (Sexton) on Jul 07, 2000 at 22:33 UTC
    Ok, the other monks handled this pretty comprehensively, but you need to be careful about interactive console mode apps and some (very few, but I've seen it on ancient 16 bit crap) windows apps - system() won't return until they exit. If you do this:
    system("isql /Speterj /Usa /Pas_if"); system("isql /Speterj_build /Usa /Pas_if");
    You'll get one instance of isql.exe instead of 2. The second instance won't start up until the first one exits. What you need to do is this:
    system("start isql /Speterj /Usa /Pas_if"); system("start isql /Speterj_build /Usa /Pas_if");
    For most Win32 Gui apps, system() returns as soon as the program is launched. --Shoeboy
    perl -e "do {kill $java, $ada, $cobol, $pascal, $csh;} until die 'Just another perl hacker';"
RE: Launching an .exe file from Perl
by Adam (Vicar) on Jul 07, 2000 at 21:40 UTC
    c-era's got it right, but you should also check for errors. $? is the %errorlevel% returned by the call and $! is any error messages. Always check for that sort of thing.

    Also be aware that these methods hang until something returns telling perl that its ok to move on. backticks( ` ) wait until the process completes.

RE: Launching an .exe file from Perl
by BigJoe (Curate) on Jul 07, 2000 at 21:41 UTC
    You could do a quick script like:
    my @array = ('paint.exe', 'photo.exe'); foreach (@array){ system($_) or die; }
    then you can just put each program into the array and not have a clutter of system calls.

    --BigJoe
      As just mentioned on the chatterbox, you have to say
      system($_) && die $!;
      because system() returns 0 on success.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-19 18:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found