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

Edited by Corion: Changed <PRE> tags to <CODE> tags.

Okay I have some lengthy code for something really simple; I've used all the portions before (reusable :)) except what's in the
### main ###
### endmain ###

section, and had no problems. Previously that scythe was a little more handy for handling specific exit values and their significance, but anyway.

now I get this error:

Can't modify subroutine entry in scalar assignment at /net/one/home1/r +ed/dlamblin/34256/deadlock_0.5hourbak.pl line 29, near "&baitKiller;" Execution of /net/one/home1/red/dlamblin/34256/deadlock_0.5hourbak.pl +aborted due to compilation errors.
what does that mean? its right on the scythe line in the main section PERLFILE:
#!/usr/local/bin/perl5 use POSIX qw(:sys_wait_h); use IO::Handle; my %scythe; # a scythe for the REAPER (see FAQ); $SIG{CHLD} = \&REAPER; # set handling of zombie child process +es. STDOUT->autoflush; # make sure output is flushed before f +orking # setup an interupt handler # kills every pid used as a key in %scythe #################### $SIG{INT} = sub { print "\nCleaning: "; print join(", ", keys(%scythe)); print "\n"; kill('INT', keys %scythe); print "done with: "; print join(", ", keys(%scythe)); die "\n"; }; #### MAIN #### while (1) { scythe{ phork("ebbackup", "-l0", "default") } = \&baitKiller; sleep(1800); } #### END MAIN, begin defs #### sub phork { # we will call fork and return a pid. The child will exec with all +args # and suppress the child's output; my $pid; if ($pid = fork) { # fork the process; #parent return $pid; }else { #child die "CANNOT FORK!!\n" unless defined $pid; close(STDOUT); # suppressing output close(STDERR); # suppressing output {exec(@_);}; # calls exec with current @_ exit(1); # exec may maybe fail... maybe +. } } sub baitKiller { # This functions is a dummy to be used to be added to %scythe for a $ +pid # This way when the interupt is called, the process with the $pid wil +l # be cleaned up (killed) so it doesn't keep running after its parent' +s # death. If you'd rather it did, just don't assign anything in %scyt +he # for the key of it's $pid. return "Come get some!"; } sub REAPER { # we reap child processes, and post processes it. # note: this sub relies on a global %scythe # sort of... if a $scythe{$pid} is defined, it will assume that its + a # reference to a function that takes $pid and $exit_value as args # and presumably does something useful with that. Then it deletes + the # $scythe{$pid} hash entry. is it not nifty? # local vars my $pid; my $exit_value; $pid = waitpid(-1, &WNOHANG); if($pid == -1) { # no child waiting } elsif (WIFEXITED($?)) { # the process exited, get exit value. my $slot; my $state; $exit_value = $? >> 8; if (exists($scythe{$pid})) { $scythe{$pid}->( $pid, $exit_value ); delete($scythe{$pid}); } else { # we're reaping something we didn't sow or didn't care about; return; } } else { # false alarm on $pid } $SIG{CHLD} = \&REAPER; #reset signal handling. } #end sub REAPER

Replies are listed 'Best First'.
Re: Can't modify subroutine entry in scalar assignment
by bbfu (Curate) on Jan 30, 2001 at 11:02 UTC

    Looks like you forgot the $ sign on the line:

    scythe{ phork("ebbackup", "-l0", "default") } = \&baitKiller;

    Should be:

      $scythe{ phork("ebbackup", "-l0", "default") } = \&baitKiller;

    I'm not sure that's exactly your problem but it might help. Btw, use strict probably would've caught that with a more helpful error (bareword not allowed or some such).

    bbfu
    Seasons don't fear The Reaper.
    Nor do the wind, the sun, and the rain.
    We can be like they are.

      Oh man; that's what I get for staying at work for um... 14 hours and writing a lot of perl. thanks.

      -daniel

Re: Can't modify subroutine entry in scalar assignment
by InfiniteSilence (Curate) on Jan 30, 2001 at 19:42 UTC
    Sometimes, if you cannot understand the warnings that Perl is giving you, you can use diagnostics; which reveals a slightly more verbose version (with examples sometimes). Here's an abbreviated version of what diagnostics does in your code:

    You used the syntax of a method call, but the slot filled by the object or package name contains an expression that returns a defined value....

    Celebrate Intellectual Diversity

Re: Can't modify subroutine entry in scalar assignment
by ColonelPanic (Friar) on Jan 30, 2001 at 20:23 UTC
    In the future, please use code tags in preference to pre. These very long lines make the front page hard to read.

    When's the last time you used duct tape on a duct? --Larry Wall