For those more used to Perl and to command lines, it is also possible to define and alter aliases from the command line. This is done with a series of Perl function.
Aliases are defined with the
$world->alias
function. This function works
similarly to $world->trigger
(described in
Chapter 8).
The simplest way to call the function is with two arguments:
$world->alias(pattern,
substitution)
. This defines an
alias with the given pattern and
substitution. For example, our
df alias would be created as follows:
You might be wondering why we use "^df$" and not only "df". Remember that aliases are just substitutions, and the pattern is a regular expression. If the anchors (that is, ^ and $) were not present, it would match against df anywhere in the line, and substitute this df even in the middle of words. wonder would become wodrink from fountainder. At times a behaviour like this (substituting anywhere) might be desirable, but in our case we want it to substitute only the whole command df, so we use the anchors.
As an example of an alias that does not match only the exact command, consider this one:
Whenever you enter a command that starts with gra followed by a space, that part (gra and the space) is replaced by the given pattern. So if you enter gra Bob, what will be sent to the MUD is chat CONGRATULATIONS, Bob.
Since aliases are just a substitution, you can define bracketed expressions in the pattern and then use $1, $2, and so on in the substitution. Let us increment the above example to add something to the end. We will need a bracketed expression:
Example 9-3. An alias that uses bracketed expressions
$world->alias('^gra (.*)$', 'chat CONGRATULATIONS, $1!!!')
It should be easy to understand. $1 in the substituion is replaced by what goes after gra.
As mentioned in Section 9.1.1, aliases can use a s//e construct, that is, whose substitution is actually composed of Perl statements evaluated when a match is found.
To use that feature, specify an alias like this:
Example 9-4. An alias whose substitution is evaluated:
$world->alias('calc\((.*)\)', 'eval "$1"', { perleval => 1})
See the difference? There is a third parameter, enclosed in curly braces {}, with the word perleval, an arrow, and the number 1. What that means is that perleval has the value 1, or, as usual in computer languages, is true. (False would be zero.) This syntax will be explained in more detail in Section 9.2.2.
The alias allows you to write something like that: gossip 2+3 = calc(2+3) and have the result of the calculation sent to the MUD. (Or any other Perl statement to be evaluated, actually.)
To specify a case-insensitive match, use the ignorecase attribute:
Before going into editing, let us see how to get a list of all
aliases that are currently defined for the World. Just use the
$world->listalias
function without arguments. You
will be presented with a list of the currently defined aliases.
There are four columns: Num is the number of the alias. Aliases are numbered sequentially starting at zero. This number will be useful when we start editing aliases. After that, Ena tells whether the alias is enabled. Aliases that are disabled are not tried. This is a nice way to stop an alias from working, but keep it stored for later use. We will see how to enable and disable aliases later in this section. The final two columns list the pattern and substitution of the alias.
The listing produced by $world->listalias
is compact, showing all aliases but possibly truncating the pattern
and/or substitution. If you give an alias number as argument to
listalias
, it will display that alias's
information detailedly.
To edit an alias, you need to know that alias's number. (And that can be discovered with the listing functions just described.) The same function used to add aliases can also change existing ones, you just need to pass the alias number as the first argument.
Calling
$world->alias(number,
new pattern)
changes the pattern
of the alias with that number. If you want to change the pattern and
substitution, include the substitution as a third argument:
$world->alias(number,
new pattern, new
substitution)
. What if you want to change
only the substitution? Since passing only one string argument would
change the pattern, this is done in a different way. In brief, this is
how you would change only the substitution:
$world->alias(number, {
substitution => new substitution
})
. Notice that the substitution is passed as an attribute
inside the curly braces. It is also possible to change the pattern
this way, just use the attribute pattern.
One other attribute of aliases is enabled. It was mentioned briefly when we described how to list aliases. It can be set just like the other attributes and is binary, that is, takes the values true (represented by anything different from 0) or false (represented by 0). When the value of this attribute is true (which is the default), the alias is tried normally. When it is zero, commands are not tried against it. This way, disabling an alias effectively turns if off, as if it did not exist, but the alias is still saved, and can be turned on again when necessary.
Here's an example of disabling an alias (number 3 in this case):
However, there is a shorter way: the
$world->disalias
function disables the alias
whose number is passed as argument. So the example above can be
rewritten in a shorter way as:
The corresponding function $world->enaalias
enables the specified alias.
It is also possible to temporarily disable all aliases. Just use the menu
-> and this will prevent aliases from being used. This does not change the "enabled" status of any aliases, it just prevents all aliases from being executed. When you select the menu again, aliases that were enabled will be matched again, and those that were disabled will remain disabled.There are times when you want to delete an alias. This is easy
to do, use the $world->delalias
function. It
takes as argument the number of the alias you wish to delete. Be aware
that once deleted it is not possible to recover the alias (unless you
create it again). Many times just disabling the alias is a better
idea. The second thing to note is that when you delete an alias the
numbers of the other aliases may change, so be careful when you try to
delete several aliases in sequence.
It is possible to assign names to aliases. When an alias has a name, you can enable, disable, or delete it using its name instead of its number.
To assign a name to an alias, specify the name attribute when creating it:
Example 9-8. Creating an alias with a name
$world->alias('^df$', 'drink from fountain', { name => 'drink' })
You can now disable this alias with
$world->disalias('drink'). The name can also
be used in the $world->enaalias
,
$world->delalias
and
$world->listalias
functions.
It is also possible to assign a name to an existing alias. Just edit it as described in Section 9.2.2, passing the name attribute. Use this same process to change the name of an alias.
Another feature of alias names is that several aliases can have the same name. In this case, all these aliases will be treated as a single group. The functions above, when passed an alias name, will act upon all aliases of the group, that is, on all aliases with that name.
Aliases are tried from the first to the last, so in some cases
the order of the aliases matters. It is possible to move an alias to
another position with the $world->movealias
function.
The function takes two parameters: the first is the name or number of the alias that you want to move. The second is the new position that the alias will take in the list. 0 means move the the first position. If you specify a negative number or a number greater than the number of aliases, the alias will be moved to the end of the list.
If there are several aliases with the same name, only the first one found will be moved. And when an alias is moved, other aliases might move up or down to accomodate the change.