Subroutines called from other subroutines

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • goatboy
    New Member
    • Sep 2009
    • 7

    Subroutines called from other subroutines

    In an effort to get back into programming, I've decided to tackle an IRC bot in Perl. As some background info, I am using the POE::Component: :IRC module, using strict and warnings, and I'm not intending this to do anything illegal. I can assure you this is simply for my own education. I have already built one that can do some amusing little tricks, but one aspect of my current project is giving me a headache.

    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_lect ure" 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:
    Code:
    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_lect ure" code:
    Code:
    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" );
            }
    
    }
Working...