Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

system() on linux/win32

by NaSe77 (Monk)
on Apr 25, 2002 at 15:24 UTC ( #161994=perlquestion: print w/replies, xml ) Need Help??

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

greatings wise monks,

i'm writing a script that is supposed to run under win32 and linux, all works fine but i have a little problem with the following line:
system($excel,$excelfilename) ||warn "!! main : could not open $excelfilename with $excel";
which works just fine under linux but under windows blocks the script until i finish excel and than gives me the warning ...

Help please NaSe

Replies are listed 'Best First'.
Re: system() on linux/win32
by dhable (Monk) on Apr 25, 2002 at 15:55 UTC
    system returns a value of 0 when the command was successful. In your case, system returns 0 because it was successful so warn gets run. To solve the problem, check to see if the value is 0 like so:
    system( $excel, $excelfilename) == 0 or warn ".....";
    This should produce the right results. See the perlfunc on system for more information.
Re: system() on linux/win32
by TheHobbit (Pilgrim) on Apr 25, 2002 at 16:33 UTC


    perldoc -f system
    you'll find that it is normal that the script blocks until the end of the execution of the command. If you want your script go on after launching the excel command, then you must do your own fork.

    Update: see this node about fork-ing on windows.

    Leo TheHobbit
Re: system() on linux/win32
by dree (Monsignor) on Apr 25, 2002 at 22:23 UTC
    To open an excel file (.xls) on Win32, without blocking the script, try this:
    use strict; use Win32::Shell; #Win32::Shell::Execute(Operation, File, Parameters, Directory, ShowCmd +) my $file="C:\\windows\\desktop\\file.xls"; Win32::Shell::Execute("open", "$file", 1, undef, "SW_SHOW");

    Update: added code tags, larsen

      thanx fellow monk,

      rests only one little prob: i can't find win32::shell nither using ppm nor cherching the cpan nor using google .. can u give a link please?



        With PPM I see:
        PPM> search win32::shell Packages available from PPMServer: Win32-Shell [0.03] A simple extension for displaying Windows message b +oxes and
        So you can install it writing:
        PPM>install Win32-Shell
        Another way is:
      • to download the zip file from:
      • unpack it into a directory
      • from a DOS shell cd into the directory, write: ppm install --location=. Win32-Shell
Re: system() on linux/win32
by Pedro Picasso (Sexton) on Apr 26, 2002 at 01:33 UTC

    Ah HAH! You're trying to get Windows to spawn a child process. It'll never work!! ...But you can fake it.

    Long story short: system("start " . $excel, $excelfilename);

    If you want Perl to spawn your Excel process but keep on doing its jiggy Perl-thing, instead of calling Excel yourself, you should tell Windows to do it for you. This is done with the DOS "start" command. Try it out. Bring up a DOS prompt and enter "notepad". It'll come up normal. Close it and enter "start notepad". Same thing. Now here's the interesting part. Put this on the command line:

    perl -e "system('notepad'); print 'NaSe77 rules!'"

    This will bring up the notepad, but it won't tell you who rules until you close notepad. Finally try this:

    perl -e "system('start notepad'); print 'NaSe77 rules the Kingdom of Notepad, baby!'"

    The notepad is up, and the print statement functions right away. Instant fake fork. Ain't life grand?

    It took me a week and a half of looking for this on Perlmonks when I needed it. I can't remember where I found it, but props to mystery monk who originally posted this solution. -the Pedro Picasso
    (sourceCode == freeSpeech)
      I believe when you use start you can just give it the filename and it will open it with the application that's associated with it.
        Just for completeness in case someone comes here looking, it's worth mentioning that the equivalent command on OS X is open. Though of course OS X can do fork() properly.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://161994]
Approved by DaWolf
Front-paged by DaWolf
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2023-11-30 19:51 GMT
Find Nodes?
    Voting Booth?

    No recent polls found