in reply to Re^2: how to open a separate notepad
in thread how to open a separate notepad
I have a routine I've used in the past when I needed start up an external executable. This will work cross-platform on Win95, Win98, WinME, Win2k, WinXP, Linux, OSX and probably many others. The only caveat is that you must have no file named "spawn.pl" in the directory your script resides in. You will probably need to adjust the calling parameters for different OS's (It is unlikely that Linux or OSX will have an executable called "NotePad"), but in general this is pretty cross platform.
It is essentially just a fork and exec, without explicitly doing one. Useful, because it is extremely difficult to fork a program running under Perl/Tk, (under Windows, at least), This neatly sidesteps the problem.
use strict; use warnings; use Tk; my $top = MainWindow->new; $top->geometry('100x100'); $top->Button( -text => 'Open A File', -command => sub{ my $types = [ ['Text Files', [qw/.txt .text .bat/]], ['All Files', ['*']], ]; my $filename = $top->getOpenFile(-title => 'Open File' +); if (defined $filename) { spawn('Notepad.exe', $filename) } } )->pack; MainLoop; sub spawn{ # Routine to spawn another perl process and use it to ex +ecute an external program my $args = join ' ', @_; unless (-e 'spawn.pl'){ open my $spawn, '>', 'spawn.pl'; print $spawn 'exec @ARGV;'; } if ($^O =~ /Win/) { $args = '"'.$args.'"'; }else{ $args .= ' &'; } system "perl spawn.pl $args"; }
|
|---|