in reply to fork and exec a codeblock

I am confused. Why do you need to exec if the code is Perl anyway and you have control of both processes and the source for both?
package MeMz; use warnings; use strict; use vars qw(@ISA @EXPORT_OK); @ISA = qw(Exporter); # inherit the import() from Exporter.pm @EXPORT_OK = qw(); # list of things to export if asked to if (fork() == 0) { require Tk; my $mw = new MainWindow; $mw->overrideredirect(1); my $t = $mw->Label(-text => '', -bg => 'black', -fg => 'yellow')-> +pack; my $id = Tk::After->new($mw, 1000, 'repeat', [ \&refresh, $pid ]); Tk::MainLoop(); sub refresh { my $pid = shift; my @size = split "\n", `cat /proc/$pid/status`; (my $vmsize) = grep { /VmSize/ } @size; my (undef, $size) = split ' ', $vmsize; $t->configure(-text => "PID: $pid -> $size"); if ($size eq '') { Tk::exit } } } 1;
Update: s/MainLoop/Tk::MainLoop()/

Makeshifts last the longest.

Replies are listed 'Best First'.
Re: Re: fork and exec a codeblock
by Anomynous Monk (Scribe) on Mar 16, 2004 at 19:38 UTC
    require Tk; ... MainLoop;
    That's not going to work. Since the sub isn't declared at the time "MainLoop;" is compiled, it is a bareword (and the use strict will make it die with 'Bareword "MainLoop" not allowed while "strict subs" in use').

    In general you can't just replace use with require without other changes. In this case, I think saying &MainLoop(); is the only needed change. Update: and add an import Tk; after the require.

      Good catch. Actually, &MainLoop(); will still be an error since require doesn't give the module a chance to export anything, and you either have to do that manually or prefix things with the right package.

      Of course, none of this has much to do with the forking question..

      Makeshifts last the longest.