473,398 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Subroutines called from other subroutines

goatboy
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_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
  1. sub irc_msg {
  2.     my ($sender, $who, $recip, $what) = @_[SENDER, ARG0 .. ARG2];
  3.     my $nick = ( split /!/, $who )[0];
  4.  
  5. #Call lecture function
  6.     if (my ($lecture) = $what =~ /^\+lecture/) {
  7.         &begin_lecture();
  8.     }
  9.  
  10. return;
  11. }
  12.  



actual "begin_lecture" code:
Expand|Select|Wrap|Line Numbers
  1. sub begin_lecture {
  2.  
  3.         open(LESSON, $ARGV[0]) or die("Cannot find my lesson plan!\n");
  4.  
  5.         my $line;
  6.         my $channel ="#chatter";
  7.  
  8.         while ( $line = <LESSON>) {
  9.  
  10.                 if (my ($pause) = $line =~ /^\:pause (.+)/) {
  11.  
  12.                         sleep $pause;
  13.                         next;
  14.                 }
  15.  
  16.                 $irc->yield( privmsg => "$channel" => "$line" );
  17.         }
  18.  
  19. }
  20.  
May 8 '10 #1
0 1121

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Alan Little | last post by:
Is this something that has ever been suggested, discussed, debated and/or considered? By subroutine I mean a chunk of code that can be called like a function but which executes in the global space....
3
by: joe | last post by:
Hello: I am using IIS 5.0, ASP and VBScript. I am puzzled by recurrences of errors when I nest subroutines, and I'd like to know if there are rules for that which I don't know. Would functions...
7
by: djc | last post by:
I have several subroutines (all inline code) that wind up using the same database connection object variable. I have been declaring a new variable in every sub. I just now came to a point where I...
22
by: fd123456 | last post by:
Hi Tom ! Sorry about the messy quoting, Google is playing tricks on me at the moment. > Global.asax is where you normally have the Global Application > and Session variables and code to...
7
by: Microsoft | last post by:
I'm not sure where to physically place my subroutines in vb.net I get namespace and not declared errors... Imports System Imports System.Management Public Class Form1
3
by: OpticTygre | last post by:
I have a class, ProcessFiles, with several subroutines it runs for each type of file I want to "process." First, I check directories for files. Then, based on the filenames of those I find in...
8
by: BookerW | last post by:
I have a subroutine called Sub Application_BeginRequest. It is in my Global.asax.vb file. It seems to be called when the application is launched, but I am not sure how. i thought somewhere in...
0
by: BlackMustard | last post by:
Hi! I'm working on a macro for Microsoft Outlook that's supposed to export emails from a user-picked folder to an Access database, in a slightly more specific way than the standard wizard way to...
11
Kelicula
by: Kelicula | last post by:
Hey everyone! I am back. I have a script that handles the private messaging for a site I am working on and I created a subroutine to handle the sending of a message. I want to check if the "to"...
10
by: liz0001 | last post by:
Hi, I need to access an .asp subroutine file from a subdomain. i.e. the website is at www.domain.com and I want to access the same subroutines on the subdomain mobile.domain.com. The...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.