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

i have an standalone executable program called RNAfold..... which i can run via command prompt by typing the following command: rnafold <inputfile> outputfile. now i want to run the program using perl. how should i go about it ??? i tried the following command... #!C:/perl/bin/RNAfold qx/rnafold <inputfile> outputfile/; and also qx/RNAfold -p -noOUTPUTFILE/; BUT IT DOES NOT WORK !!! please help....
ALL THINGS R DIFFICULT BEFORE THEY BCOME EASY !!!
  • Comment on running a executable program using perl ???

Replies are listed 'Best First'.
Re: running a executable program using perl ???
by Corion (Patriarch) on Mar 28, 2008 at 08:40 UTC

    Traditionally, "It does not work" is a common yet very undescriptive error message. Maybe you can tell us more exactly how it does not work. For example, when approaching the issue of running an external program, I use the following code:

    #!/usr/bin/perl -w use strict; my $program = "rnafold"; my $cmd = "$program -p -noOUTPUTFILE"; system($cmd) == 0 or die "Couldn't launch [$cmd]: $!/$?";

    This usually gives me an informative error message about what happened. Maybe you can do the same and tell us about the error message.

    As an aside, there seem to be some problems with your keyboard. Please have a technician check your CAPS Lock key and your "A" and "E" keys. They seem to become stuck or not to register at all at times.

      (Totally off-topic) The technician should also check the "?" key, as it seems to always send 3 keystrokes at once.

      Just noticed: Let him check "!" and "." too ;-)


      s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
      +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
Re: running a executable program using perl ???
by tachyon-II (Chaplain) on Mar 28, 2008 at 11:16 UTC

    az noted sAN duz not wrk & ***SHOUTING*** duz not hlp U 2 git Ansz. az we hav d luxury of full [abc]z 2 typ w & usrz 4 huM eng iz not thR 1st lngwij UzN SMS shrt& iz gnrle :-(D upon transl8it here.

    Look below when you post and you will see some Writeup Formatting Tips. In Tutorials there are lots of how to ask questions the smart way hints.

    Anyway you are obviously on windows and seem to want to apply to power of Perl to automating some stuff for you. Perl can do everything a .bat batch file can and lots more.

    To get windows to run a perl script you just make a text file that contains perl code and save it with a .pl extension. If you type script.pl at the command line windows looks at the .pl extension and knows you want perl to execute the script using the perl executable. You can also type C:\>perl script which explicitly tells windows to execute the file <scipt> using perl and lets you call the script anything you want, using any extension you feel like. As a rule .pl makes sense so use it.

    In unix the shebang line tells the shell where to find perl. In your application the only significant thing on the usual first line of a perl script (called the shebang line) is the -w flag which switches on warnings. Any script you write should probably start like this:

    #!/usr/bin/perl -w use strict

    This switches on warning and strict which makes you write code where perl catches lots of errors for you.

    To execute an external binary perl gives you three options. backtics, system and exec. You can RTFM to find out the finer details.

    #!/usr/bin/perl -w use strict; my $prog = "C:\\path\to\\RNAfold"; my $infile = "C:\\path\\to\\infile"; my $outfile= "C:\\path\\to\\outfile"; my @args = qw/ -p noOUTPUTFILE /; print "Executing with one arg system\n"; my $return_value = system("$prog $infile $outfile"); if ($return_value == 0) { print "Success!\n"; } else { printf "Failed with error code %d\nError message: $?\n", $return_value>>8; } print "Executing with muti arg system\n"; $return_value = system($prog, $infile, $outfile); # or $return_value = system($prog, @args); # check how it went by checking return_value as above print "Executing $prog $infile $outfile with backtics\n"; my $screenoutput = `$prog $infile $outfile`; print "Got $screenoutput\n"; # check how it went by checking $? die "Got error $?\n" if $?
Re: running a executable program using perl ???
by ww (Archbishop) on Mar 28, 2008 at 17:21 UTC

    Slightly reformatted and ellipsized for easier (YMMV) comprehension, thinkdifferent wrote:

    ...I can run via command prompt by typing the following command:
    rnafold <inputfile> outputfile.
    now i want to run the program using perl.... i tried the following command...
    #!C:/perl/bin/RNAfold qx/rnafold <inputfile> outputfile/;

    You say "command" in both examples, but while the former apppears to be a valid, windows command line input, the latter looks like a borked fragment of a script. If you really mean you tried to type
    #!C:/perl/bin/RNAfold qx/rnafold <inputfile> outputfile/; at a W32 command line to run a Windows "executable", the shebang isn't going to magically invoke Perl (in fact, IIRC, it should cause your system to spit an error to the general effect of "'#!C:/' is not a valid program"). What's more, even if that worked, the balance of the line isn't going to utilize qx/.../ without invoking perl.

    So, please clarify: is the second example meant to be the code in some script you've created? ...Or is a case of making stuff up and hoping the computer will understand?

Re: running a executable program using perl ???
by zentara (Cardinal) on Mar 28, 2008 at 16:47 UTC
    I don't use windows, but if you are taking an input file and writing to an output file, you are better off running it in a separate thread. On linux, we usually call it "fork and exec", but on windows, its all done with threads. The reason is that system or exec will take control over your script until the program is done....whereas the following will let you launch it, and continue with the Perl program. Otherwise, why even run it from Perl? Here are some nodes to show you

    Running a process in the background

    MS Forking Blues

    They didn't give me a fork so I have to eat with a spawn.


    I'm not really a human, but I play one on earth. Cogito ergo sum a bum