Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Start an MS window in background from a perl script

by rlambert7 (Acolyte)
on Sep 19, 2007 at 05:51 UTC ( #639814=perlquestion: print w/replies, xml ) Need Help??
rlambert7 has asked for the wisdom of the Perl Monks concerning the following question:

I want to start "notepad" in the background from a perl script. On my PC I have both Cygwin and ActivePerl. This is my test script:

system("notepad.exe &")

Actually, I have two scripts. The only difference between the two is the very first line in the script.

The script on Cygwin starts with:

#!/usr/bin/perl

The ActivePerl script starts with:

#!c:/Perl/bin/perl

The Cygwin script does just what I want, it starts a Notepad window, and the script terminates. The ActivePerl script starts a Notepad window, but also brings up a little message window stating that it can't find the file "&.txt", and the perl script is waiting on the Notepad window. Obviously, it "thinks" I am passing the file name "&" as the file to open with Notepad. What's wrong? What do I have to do to make the ActivePerl script open the Notepad window in the background (like it does with the Cygwin Perl script)?

Thanks.

  • Comment on Start an MS window in background from a perl script

Replies are listed 'Best First'.
Re: Start an MS window in background from a perl script
by ikegami (Pope) on Sep 19, 2007 at 05:56 UTC

    & is a sh thing. The same can be achieved using start in cmd.

    Update: Better yet, the Windows-specific system(1, "...command...");.

      & is a sh thing

      But, in a sense, that's an oversimplification. I mean, if I run (in Cygwin's bash shell):
      Rob@desktop2 ~ $ perl -e 'system "notepad &"'
      then that works as intended. But if I invoke ActivePerl (in the very same Cygwin shell) by running:
      Rob@desktop2 ~ $ /cygdrive/c/_32/ap822/bin/perl -e 'system "notepad &"'
      then I get the precise (undesired) behaviour as described by the op - where I get told that &.txt does not exist.

      Is it simply a case of "ActivePerl always assumes that it's being run in the cmd shell" ? ... or is there a more erudite way of explaining this ?

      Cheers,
      Rob

        There's no assumption being made as to which shell launched Perl. As documented, the shell used by system is constant.

        On most platforms, that shell is /bin/sh (no matter what the user's login shell is or which process launched perl), so system's argument must be a sh command. & can be used to run a background task in sh.

        On Windows (cygwin aside), that shell is cmd, so system's argument must be a cmd command. start can be used to run a background task in cmd.

      Thanks everyone for your posts. The suggestion of using 'system(1, "...command...");" worked. THANKS.

      Interestingly, before I even tried to use "system", I thought there might be more than one parameter I could pass to it, but I couldn't find documentation on it, so please let me ask a couple of questions about Perl documentation.

      I tried "perldoc system", but it said there was no documentation on "system". Where/how could I find the syntax for "system", and a brief synopis?

      Also, how can I list all of the functions contained in a Perl module, and how do I find a synopsis and syntax of a specific function within a module? (For example, "perldoc Win32::TestGui" gives me some information, but not all of what I would need to effectively use that module).

      Thanks, again.

        To get information on functions from perldoc you use the -f switch.

        So it's

        perldoc -f system

        OS-specific differences are documented in perlport.

        Your last paragraph is really unrelated. Why don't you start a new SoPW thread?

Re: Start an MS window in background from a perl script
by andreas1234567 (Vicar) on Sep 19, 2007 at 06:20 UTC
    Consider Win32::Process. The synopsis looks very much like your notepad example.
    --
    Andreas
Re: Start an MS window in background from a perl script
by pKai (Priest) on Sep 19, 2007 at 10:40 UTC

    The ampersand thing (-> put the process into the background) is something specific to nix (so will work under Cygwin), which will not be understood by MSWin.

    A convenient wrapper around this distinction is the Proc::Background module, which will fork under nix and use Win32::Process under Win, depending on the hosting OS.

Re: Start an MS window in background from a perl script
by duff (Vicar) on Sep 19, 2007 at 14:11 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2016-09-30 06:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Extraterrestrials haven't visited the Earth yet because:







    Results (562 votes). Check out past polls.