Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

call another program from perl

by lil_v (Sexton)
on Jul 14, 2008 at 17:58 UTC ( #697531=perlquestion: print w/ replies, xml ) Need Help??
lil_v has asked for the wisdom of the Perl Monks concerning the following question:

Hey, I am trying to call a simple .exe program from perl and I'm trying to input a statement to it (alot like a command prompt). I am wondering if it was possible to do it with the filehandles? Here is my code:
my $executable = "C:/Users/Desktop/file.exe"; open(-w EXECUTABLE,">$executable") or die "Can't open output file $exe +cutable\n"; while(<EXECUTABLE>) { print EXECUTABLE "Something"; } close EXECUTABLE;
The print statement isn't right but i wrote it so it gives somewhat of an idea.

Comment on call another program from perl
Download Code
Re: call another program from perl
by Fletch (Chancellor) on Jul 14, 2008 at 18:10 UTC

    What have you tried so far? What part of the documentation for system, open, and interprocess communication is giving you difficulty? What is the airspeed velocity of an unladen swallow? Why do people drive on parkways and park on driveways?

    Update: This posted before the OP was stealth updated with actual code . . .

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      I couldn't resist....

      if 7/11 (a quick mart) is open 24 hours, 7 days a week... why do they have locks on the doors?

        This question is probably rhetorical but I'll answer anyway (what if there were no hypothetical situations?). A while back, my wife worked at a restaurant named "Jojo's". It was open 24/7 and had no locks on the doors. Then, they got robbed and the police wanted to secure the area. Uh oh.

        --
        Wade
        The real reason is that oftentimes at gas stations etc like that, there is only one person working there. If they need to go to the bathroom, or say... leave the store to go outside to exchange propane tanks for someone, they have to make sure nobody is inside, and then they lock the doors for 5 minutes or whatever.
Re: call another program from perl
by zentara (Archbishop) on Jul 14, 2008 at 18:29 UTC
      I tried using system() and I get an error. Here is my code:
      my $executable = "C:/Users/Desktop/file.exe"; @args = ("$executable", 'input_command'); system(@args) == 0 or die "system @args failed: $?"
      Here is the error I get:
      An endfile record was detected in a READ statement (unit= 5). Error occurs at or near line 21201 of _opnfil_ Called from or near line 231 of _MAIN__
      Any suggestions?
Re: call another program from perl
by CountZero (Bishop) on Jul 14, 2008 at 21:45 UTC
    What does the docs for "C:/Users/Desktop/file.exe" say? Will this program accept input through STDIN, files, pipes or command line options? Perhaps it expects voice commands and then you will have to play it a sound file!

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: call another program from perl
by oko1 (Deacon) on Jul 15, 2008 at 03:35 UTC

    Please do us all the courtesy of at least trying to execute your code before you post it, and fixing any errors that may come up.

    ### No reason to do this, since you could just use the filename in the + "open" call directly. my $executable = "C:/Users/Desktop/file.exe"; ### This won't work - that "-w" is extraneous and a syntax error. Furt +hermore, you have a conceptual problem: ### the way you're doing it here (if it worked) would OVERWRITE your p +rogram instead of ### executing it. open(-w EXECUTABLE,">$executable") or die "Can't open output file $exe +cutable\n"; while(<EXECUTABLE>) { print EXECUTABLE "Something"; } close EXECUTABLE;

    Yes, it's possible to do something like what you've described with filehandles (e.g., "Pipe Opens" in perlopentut.) Until you explain what you're trying to do, though, there's not much chance of anyone here being able to help you.

    
    -- 
    Human history becomes more and more a race between education and catastrophe. -- HG Wells
    

      Actually putting the program name in a variable is an excellent step for several reasons:

      • It's no longer a hard-coded constant
      • It can be reused (for example in the error message when the open call fails)
      • this also prevents "constant drift" where you'd have to remember to change it in both the parameter to open as well as your error message

      Especially if the location is likely to vary from machine-to-machine (or user-to-user) you might want to go even a step further by defining the default location as a constant (or Readonly value if you like) and initializing the path using that constant but allowing it to be overridden via Getopt::Long option or the environment.

      Yeah the rest of the OP's code has problems, but this complaint's specious.

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

        Your corrections would be true if this was a program of any length. As it stands, you're picking nits. This is like insisting that a one-liner consisting of 'print "Hello, world"' must enable strictures, include a test suite, and be fully documented.

        
        -- 
        Human history becomes more and more a race between education and catastrophe. -- HG Wells
        
      ouch! I deserve all the criticism, this was probably my worst node. I was reading everything and I couldn't make sens out of it so I wrote something that kind of made sens to me. Anyways, I just started learning Perl so you guys have to give me some credit :) Back to the program, the file.exe opens up like command prompt and in the folder where file.exe is, there is an input file "base.in". So, I need to open file.exe through perl and type in "base" in file.exe for it to take the input file and run. The input file is created by the Perl program. Instead of taking the input file and passing it to file.exe, I'm trying to pass it through the perl program. After running, the program outputs text files into the folder. I would need to read the text file on Perl as well but I guess that's for another time ;).
        Back to the program, the file.exe opens up like command prompt and in the folder where file.exe is, there is an input file "base.in". So, I need to open file.exe through perl and type in "base" in file.exe for it to take the input file and run.

        (additional emphasis by me.)

        I personally believe that the info you supplied is unfortunately still incomplete. It is enough to exclude that you have to use system because of that "to type in" - but whether a piped open is appropriate or not depends entirely on the method your file.exe uses to read what you type in. If it reads from STDIN then it's fine, and you only have to read the appropriate documentation linked to above: wrt the naive attempt you shown in the first post the main difference will be a the correct "mode" which is a simple string saying what to do with the filename. (Additionally, you used the two args form of open() in which the mode is attached to the filename itself, and I recommend as usual to use the three args one instead.) Specifically, you used ">" which means "write into" - and that's no good! Perl and the OS "know nothing" about the file itself to the effect that it is actually a program, and "write into" will literally do so, filling its contents with e.g. "base.in"; of course, this is not what you want, thus you have to explicitly tell it that it's a program and that you want to "write to it through STDIN." HTH: filling in the details is left as an easy exercise.

        Incidentally, your original code also had open(-w EXECUTABLE,">$executable") and that looks very incorrect: it is, somewhat surprisingly, syntactically valid due to the existance of the -w function, but that -in turn- is nothing but a test that a given handle is readable, and does not belong there, by any means.

        --
        If you can't understand the incipit, then please check the IPB Campaign.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (11)
As of 2014-09-16 14:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (24 votes), past polls