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

Hey fellow Monks,

I have been using system(abs_path_to_file); to "execute" files here in Windows. It all works fine, execpt that this is ran in a thread, and since the thread takes time to start (be created), it takes a while before the file is actually run. Now, I used this since this way the file is ran also with the corresponding/associated application. That is, if its a mp3, it would run with.. say Winamp. Now, as I said, I have to use threads since its supposed to run seperatly, and not make Perl wait for this process to finish. However, as I mentioned, I dont get direct resopose, since the thread has to be created (takes a few seconds). Proc::Background seems to solve this thread requirement, but that module just expects .exe files! So, what to do?

What it comes down to is that I wanna run anything and make windows start that like if I used cmd. And also, let Perl continue with its bisuness!

run_me("c:/textfiles/hello.txt"); # this would open the .txt file in notepad, if you have the extension set to that application.

Thanks,
Ace
  • Comment on Proc::Background (or runing something seperate)

Replies are listed 'Best First'.
Re: Proc::Background (or runing something seperate)
by Thelonius (Priest) on Sep 06, 2005 at 03:37 UTC
    If there are no spaces in the path name and the file's permissions are set to allow execute access you can do this:
    system("start $filename");

    But the better way to do it is to install Win32::API via the PPM command (or CPAN); then you can do this:

    use Win32 qw(SW_SHOWNORMAL); use Win32::API; use strict; my $FILE_OR_URL = ... whatever ...; my $shellexec = Win32::API->new('shell32', 'ShellExecute', 'NPPPPI', 'N') or die "cannot import ShellExecute: $!\n"; my $result = $shellexec->Call(0, "open", $FILE_OR_URL, "", ".", SW_SHOWNORMAL); if ($result < 33) { print STDERR Win32::FormatMessage($result), "\n"; exit(1); }
      Thanks, but I was hoping for a more multios solution... But I guess this will do... :)
      Btw, wanna explain some of the code there? NPPPPI?
Re: Proc::Background (or runing something seperate)
by zentara (Cardinal) on Sep 06, 2005 at 13:22 UTC
    and since the thread takes time to start (be created), it takes a while before the file is actually run.

    What I do is create the worker thread once during the startup of the script, and put the thread to sleep. Then when I want it to do something, I use threads:shared to turn it on and pass it a string to be used (a mp3 name).

    If it is only mp3 files you want to play, you might be able to use a piped open to your mp3 player, and just feed it filenames on STDIN. I've done this in linux. (I realize you are on windows, but this just demonstrates the idea)

    #!/usr/bin/perl use warnings; use strict; use Tk; #read README.remote in mpg123 sources $|++; #my $pid = open(FH,"| mpg123 -R 2>&1 "); my $pid = open(FH,"| mpg123 -R >/dev/null 2>&1 "); print "$pid\n"; my $mw = new MainWindow; my $startbut = $mw->Button(-text =>'Start Play', -command => sub{ syswrite(FH, "LOAD 1bb.mp3\n") ; })->pack; my $pausebut = $mw->Button(-text =>'Pause', -command => sub{ syswrite(FH, "PAUSE\n") ; })->pack; my $exitbut = $mw->Button(-text =>'Exit', -command => sub{ exit })->pack; MainLoop;

    I'm not really a human, but I play one on earth. flash japh