9.2. Using Aliases

In Chapter 9, Aliases an alias that replaces dt with drink from fountain was described. To create this alias, enter the following parameters, leaving the other options unchanged from the default values:

Example 9.1. A simple alias
  • Pattern: ^df$

  • Substitution: drink from fountain

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:

Example 9.2. A slightly more complex alias
  • Pattern: '^gra ' (without the quotes, but note the space after the word)

  • Substitution: 'chat CONGRATULATIONS, ' (without the quotes, but note the space after the word)

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
  • Pattern: ^gra (.*)$

  • Substitution: chat CONGRATULATIONS, $1!!!

It should be easy to understand. $1 in the substituion is replaced by what goes after gra.

9.2.1. Advanced Features

As mentioned in Section 9.1.1, “Adding Aliases”, 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, select the Eval substitution as Perl statement option:

Example 9.4. An alias whose substitution is evaluated:
  • Pattern: calc\((.*)\)

  • Substitution: eval "$1"

  • Eval substitution as Perl statement: checked

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

By default, case is considered when matching. If you want case-insensitive matching, select Ignore case when matching.

9.2.2. Using Perl in Aliases

It is possible to make an alias whose action is not to send a changed command line, but to execute an action.

To do that, make the replacement start with /, which means that the rest of the line will be executed, and not sent to the mud. In the rest of the line, you can put arbitrary Perl code. However, if you want to refer to a variable, you must escape the $ prefix by writing it as \$. This way, when the substitution is run, you get the name of the variable, not the value this variable currently has.

As an example, if you define an alias with pattern ^say (.*)$ and action /\$world-\>echonl("$1"), whenever you type a line that starts with say in the input box, the rest of the line will be echoed in the screen, but not sent to the mud.