Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Battle script

by Abigail-II (Bishop)
on Jun 06, 2002 at 16:16 UTC ( #172268=note: print w/replies, xml ) Need Help??


in reply to Re: Battle script
in thread Battle script

Of course, one doesn't need those newfangled thread thingies. Here's one that can easily be backported to old versions of perl, and is using fork() and signals.

#!/usr/bin/perl -w my $hp = 100; my $pid; sub get_hit; sub get_hit { unless (-- $hp) { kill USR2 => $pid or warn "Failed to send victory: $!\n"; print "$$: Arrrgh, I am dying!\n"; exit; } print "$$: I got hit! $hp hitpoints left.\n"; $SIG {USR1} = \&get_hit; } $SIG {USR1} = \&get_hit; $SIG {USR2} = sub {print "$$: I won! I won!\n"; exit}; $pid = fork; die "Fork failed\n" unless defined $pid; $pid ||= getppid; while (1) { my $timeleft = rand 1; (undef, $timeleft) = select (undef, undef, undef, $timeleft) while $timeleft; kill USR1 => $pid; # Ignore errors. } __END__
Abigail

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://172268]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2021-08-03 01:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My primary motivation for participating at PerlMonks is: (Choices in context)








    Results (31 votes). Check out past polls.

    Notices?