## 9.3. Defining Aliases in the Command Line

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, Triggers).

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, the alias described in Example 9.1, “A simple alias” would be created as follows:

Example 9.5. A simple alias defined in the command line

    $world->alias('^df$', 'drink from fountain')

Like the `$world->trigger` function (see Section 8.6, “Defining Triggers in the Command Line”), you can specify extra arguments to control advanced alias features. One such argument is `perleval` that causes the substitution to be evaluated as a Perl statement. The alias defined in Example 9.4, “An alias whose substitution is evaluated:” can also be created as follows:

Example 9.6. An alias whose substitution is evaluated (in the command line)

    $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.)

To specify a case-insensitive match, use the `ignorecase` attribute.

### 9.3.1. Editing Aliases

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. Alternatively, the `$world->getaliasnumber` can be used to discover an alias's number if its name is known.) 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):

Example 9.7. Disabling an alias, the long way

    $world->alias(3,
                  { enabled => 0 })

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:

Example 9.8. Disabling an alias, the short way

    $world->disalias(3)

The corresponding function `$world->enaalias` enables the specified alias.

It is also possible to temporarily disable all aliases. Just use the menu Preferences → Disable Aliases 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.

### 9.3.2. Assigning Names to Aliases

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.9. 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.3.1, “Editing Aliases”, 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.

### 9.3.3. Reordering Aliases

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.