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

Ok thanks a lot guys for your response.. But one more question on opening programs. Suppose you want to open programs with a particular file. As SamCG said <code? perl -e "system 'C:\Program Files\TextPad 4\Textpad.exe'"</code> open the textpad in his computer. But suppose i want to open the textpad with a particular file say myfile.txt Then? The code that you might give me... can that be extended to other programs also like for eg. opening Winamp with a particular mp3 ?

Replies are listed 'Best First'.
Re: How to use perl to open programs
by SamCG (Hermit) on Jan 20, 2006 at 22:07 UTC
    well, you're not all that specific if you need to interact with them, so. . .
    perl -e "system 'C:\Program Files\TextPad 4\Textpad.exe'"
    Works on my xp system to open textpad...
Re: How to use perl to open programs
by ikegami (Patriarch) on Jan 20, 2006 at 22:07 UTC
Re: How to use perl to open programs
by jZed (Prior) on Jan 20, 2006 at 22:08 UTC
    If it's a command-line program: system("abcde.exe"). If it's a windows program: system("start","abcde.exe").
      hum? Why use start for one and not the other? start will cause the Perl script to not wait for the application to finish whether it's a console application or a Windows application.
        Sorry, my windoze is a bit rusty. I seem to remember that start was required for some non-console programs.
Re: open programs with a particular file!
by BrowserUk (Patriarch) on Jan 21, 2006 at 03:42 UTC

    If the program you want to start is associate with the filetype of the file you want it to load, then a simple

    system 'path\to\file.ext';

    will start the default application for that filetype, passing it the filename via it's command line.

    The perl script will not wait for the program started to end if it is a GUI app. All the usual caveats about backslashes, spaces in pathnames etc., apply. This is an implicit 'start' command being issued by cmd.exe, and you should read the help for that command for further info.

    If you want to open a file in a particular application, which is not the current default for that filetype, then you will have to use both the command and the filename on the command line in the normal way.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: open programs with a particular file!
by holli (Abbot) on Jan 21, 2006 at 10:35 UTC
    That depends on the program you are trying to call. Look up the programs documentation if and how you can specify a command line parameter to make it open a file after startup. In the case of textpad it's simple. Simply pass it the name of the file.
    system ("textpad.exe", "myfile.txt");


    holli, /regexed monk/