4.3 Creating a procedure
With the proc command you can create procedures.
The syntax of the proc command is proc <name> { <parameters> } { <body> }.
The <name> is how you want to call the procedure you are creating.
This can be anything you want and you will have to use this name when you want to call upon it.
In this guide I will start every procedures name with <bind type>:. This isn't required, but it's (in
my opinion) handy to do so, because this way you immediantly know what a proc is for which is especially handy
when you are working with large scripts.
The <parameters> are the variables the procedure must put its received parameters in.
You need to specify a variable for each parameter here that will be sent to the procedure. All the variables
you give have to be seperated by spaces.
For example, if you would have the line proc test { nick channel } { <body> } and somewhere
else in your script test MyNick #test, the procedure would put "MyNick" in the
variable "$nick" and "#test" in the variable "$channel".
The procedure also always wants to know the exact amount of parameters it is given. If you give the
procedure 4 variables for example and you call upon it with 5 parameters, Eggdrop will give an error similar to
'proc called with too many arguments' or if you call upon it with 3 parameters and have given it 4 variables, it
will give an error similar to "no argument given for ...".
There is one exception to this rule though. If you call the last variable of <parameters> "args" than you may
call upon the procedure with more parameters than you have defined in <parameters>.
In this case all of the parameters you give to the procedure up from the point where the argument "args"
start is put in $args. They are put into $args as if the list command was used to make $args. You'll
learn what the list command is later on, but you should already know about the difference between "args"
and any other name.
For example if you would call upon proc test { nick channel args } { <body> } with
test $nick $channel $handle $host, it would put the passed on $nick in $nick, $channel in $channel and
both $handle and $host in $args, but I strongly discourage using the "args" parameter until you understand what
lists are and what the list command does.
The <body> is basicly the commands that you want the procedure to execute.
The body doesn't have to be on one line, that's why the proc starts with an open-brace. You can put a new
command on each new line and it'll still be part of the procedure until you close the body up with a
close-brace.
Besides calling upon procedures yourself, you will most likely want to use bind aswell.
The bind command runs a procedure when a certain event is triggered. Besides information about the bind,
tcl-commands.doc also gives you information about with what parameters the procedure will
be called upon when the bind is triggered and what information the parameters will contain.
Lets take a look at the bind msg explanation from tcl-commands.doc:
(1) MSG
bind msg <flags> <command> <proc>
procname <nick> <user@host> <handle> <arg>
Everything behind procname are the parameters that will be sent to the procedure. Most procedures for
bind msg will thus look like proc msg:test { nick host hand arg } { <body> }.
|