in reply to call another program from perl

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

Replies are listed 'Best First'.
Re^2: call another program from perl
by Fletch (Bishop) on Jul 15, 2008 at 12:46 UTC

    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
      
Re^2: call another program from perl
by lil_v (Sexton) on Jul 15, 2008 at 12:51 UTC
    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.