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

Hi, I am working on Perl on Windows(command prompt). I have one .exe file "sqlpplus.exe" in my C drive. I need to write a script where I'll first check whether this file is present or not. If yes, then run this file. Here is the code snippet
#!usr/bin/perl my ($sec,$min,$hour,$day,$month,$yr19,@rest) = localtime(time);##### +##To get the localtime of your system print "Date:\t$day-".++$month. "-".($yr19+1900)."\n"; ####To print dat +e format as expected print "Time:\t".sprintf("%02d",$hour).":".sprintf("%02d",$min).":".spr +intf("%02d",$sec)."\n";###To print the current time $SQLPLUS = "C:/sqlplus.exe"; if( -f$SQLPLUS ) { print " exe file is present\n"; } else { die "file is not present\n"; } The code to run the sqlplus.exe.......shoud be here . . . . .
I do not know how to invoke or run this file. Please assist. Thanks Ethen

Replies are listed 'Best First'.
Re: How to run a .exe file from a Perl script?
by marto (Cardinal) on Jan 02, 2008 at 12:00 UTC
    Ethen,

    Have you looked at system? However, is there a particular reason why are you interacting with Oracle in this manner? I would suggest taking a look at DBI which is (unless there is a reason why you want to run the exe with arguments) a better way or interfacing with your database, depending on what you want to do.

    Hope this helps

    Martin
Re: How to run a .exe file from a Perl script?
by bart (Canon) on Jan 02, 2008 at 12:18 UTC
    Hmm, I've got more comments than just an answer to your question.
    my ($sec,$min,$hour,$day,$month,$yr19,@rest) = localtime(time);##### ##To get the localtime of your system print "Date:\t$day-".++$month. "-".($yr19+1900)."\n"; ####To print dat +e format as expected print "Time:\t".sprintf("%02d",$hour).":".sprintf("%02d",$min).":".spr +intf("%02d",$sec)."\n";###To print the current time
    Why are you not simply using strftime in POSIX? It is like using sprintf but for date and time formats. It can format the time any way you want without you having to tweak the fields from localtime, first.

    And even if you decide not to, it's still clearer to use a single sprintf (or printf) call, instead of concatenating results of several calls.

    printf "Time:\t%02d:%02d:%02d\n", $hour, $min, $sec;###To print the cu +rrent time
    $SQLPLUS = "C:/sqlplus.exe";
    You're hardcoding a single location, instead of letting Windows find it in your PATH.

    You can check if it exists by using:

    $version = `sqlplus -V`;
    Either that will succeed, putting a string with a version number in $version, or it'll fail, returning undef and probably setting $? too to a nonzero value.

    If you insist of finding exactly where SQLPLUS is, take a look at `which` (for Windows) in pure perl, which implements a Pure Perl snippet for finding an executable in PATH.

    The code to run the sqlplus.exe.......shoud be here
    Like I showed just above, you can use backticks (AKA qx), which will capture the STDOUT of the program; you call system which will let STDOUT go its usual way; or you can use one of a few modules, like IPC::Open2, IPC::Open3, IPC::Run. You can even use open with a trailing "|" after the command line, to read STDOUT from the running program. Oh, and on Windows, you can use Win32::Process too, which will not wait for the invoked program to finish, to resume execution of your script. Choices, choices...
Re: How to run a .exe file from a Perl script?
by FunkyMonk (Bishop) on Jan 02, 2008 at 12:03 UTC
    Use system to run it:
    system( $SQLPLUS ) == 0 or die $?;

    Or, if you want to capture the output of the program use backticks, or the qx{} equivalent.

    my $output = qx{ $SQLPLUS };

Re: How to run a .exe file from a Perl script?
by IL_MARO (Initiate) on Nov 09, 2008 at 09:17 UTC
    Thank you Perl Monks, you shared your wisdom -> I have solved many problems just looking at posted threads and tutorials.
    Here is my question: I run an executable from the DOS-like command line, just as the guy who started this thread.
    The problem is that I have to start it many times (running the same executable) and each time the exe command will take a different time to perform its task.
    I have to run the exe for a fixed, known number of times. This means that I need PERL commands to
    (1) run the file from command line (ok, I got it...)
    (2) check if the exe has finished the work
    (3) if yes, go back to (1)

      system will wait until the program has finished and then continue your Perl program.

        Actually I discovered it just a few moments after posting the topic... :-|
        Thank you anyway :-)