I'm having some trouble with subroutines. Specifically, there are methods in the IRC module I am using called "irc_msg" and "irc_public" which are called when you receive a private message and when someone speaks in the channel, respectively. When those are called, I check to see if the message is a bot command, in this case "+lecture"
If the whisper/message matches the command, a subroutine named "begin_lecture" is called. This sub implements a while loop to read a file (called a "lesson plan") line-by-line, posting each line as a new message to the channel.
If the line happens to be ":pause X" where X is a number, the output will be delayed for X seconds. This is so I can give readers some time between messages.
The problem I am having is that, although the function is called properly when I say/whisper "+lecture" it is not being executed how I would expect. Instead of printing a line, pausing, printing, pausing, etc., it waits the total number of seconds, then outputs all messages at once.
I believe this is because the output of the while loop is being returned to the calling function, effectively causing a huge delay followed by a wall of text.
Is there a way I can "fork" the begin_lecture sub (not sure if fork is the right word) to fix this behavior? Ideally, it should appear as if the text is being typed, with appropriate pauses in between each message.
I have supplied the relevant code below:
irc_msg:
Expand|Select|Wrap|Line Numbers
- sub irc_msg {
- my ($sender, $who, $recip, $what) = @_[SENDER, ARG0 .. ARG2];
- my $nick = ( split /!/, $who )[0];
- #Call lecture function
- if (my ($lecture) = $what =~ /^\+lecture/) {
- &begin_lecture();
- }
- return;
- }
actual "begin_lecture" code:
Expand|Select|Wrap|Line Numbers
- sub begin_lecture {
- open(LESSON, $ARGV[0]) or die("Cannot find my lesson plan!\n");
- my $line;
- my $channel ="#chatter";
- while ( $line = <LESSON>) {
- if (my ($pause) = $line =~ /^\:pause (.+)/) {
- sleep $pause;
- next;
- }
- $irc->yield( privmsg => "$channel" => "$line" );
- }
- }