File: ch09s02.xhtml

package info (click to toggle)
kildclient 3.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 6,320 kB
  • sloc: ansic: 24,830; xml: 7,500; sh: 4,295; perl: 2,877; makefile: 172
file content (53 lines) | stat: -rw-r--r-- 8,271 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><title>9.2. Using Aliases</title><link rel="stylesheet" type="text/css" href="docbook.css"/><link rel="stylesheet" type="text/css" href="kildclient.css"/><meta name="generator" content="DocBook XSL Stylesheets V1.79.1"/><link rel="prev" href="ch09s01.xhtml" title="9.1. Creating and Editing Aliases"/><link rel="next" href="sec_alias_cmdline.xhtml" title="9.3. Defining Aliases in the Command Line"/></head><body><header><div class="navheader"><table style="width: 100%; "><tr><th style="text-align: center; " colspan="3">9.2. Using Aliases</th></tr><tr><td style="width: 20%; text-align: left; "><a accesskey="p" href="ch09s01.xhtml">Prev</a> </td><th style="width: 60%; text-align: center; ">Chapter 9. Aliases</th><td style="width: 20%; text-align: right; "> <a accesskey="n" href="sec_alias_cmdline.xhtml">Next</a></td></tr></table><hr/></div></header><section class="sect1" id="idm1623"><div class="titlepage"><div><div><h2 class="title" style="clear: both">9.2. Using Aliases</h2></div></div></div><p>In <a class="xref" href="chap_aliases.xhtml" title="Chapter 9. Aliases">Chapter 9, <em>Aliases</em></a> an alias that
replaces <strong class="userinput"><code>dt</code></strong> with <strong class="userinput"><code>drink from
fountain</code></strong> was described. To create this alias, enter the
following parameters, leaving the other options unchanged from the
default values:</p><div class="example" id="ex_simple_alias"><div class="example-title">Example 9.1. A simple alias</div><div class="example-contents"><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>Pattern: <code class="literal">^df$</code></p></li><li class="listitem"><p>Substitution: <code class="literal">drink from fountain</code></p></li></ul></div></div></div><br class="example-break"/><p>You might be wondering why we use "<code class="literal">^df$</code>" and
not only "<code class="literal">df</code>". Remember that aliases are just
substitutions, and the pattern is a regular expression. If the anchors
(that is, <code class="literal">^</code> and <code class="literal">$</code>) were not
present, it would match against <strong class="userinput"><code>df</code></strong> anywhere in
the line, and substitute this <strong class="userinput"><code>df</code></strong> even in the
middle of words. <strong class="userinput"><code>wonder</code></strong> would become
<strong class="userinput"><code>wodrink from fountainder</code></strong>. At times a behaviour
like this (substituting anywhere) might be desirable, but in our case
we want it to substitute only the whole command
<strong class="userinput"><code>df</code></strong>, so we use the anchors.</p><p>As an example of an alias that does not match only the exact
command, consider this one:</p><div class="example" id="idm1651"><div class="example-title">Example 9.2. A slightly more complex alias</div><div class="example-contents"><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>Pattern: <code class="literal">'^gra '</code> (without the
    quotes, but note the space after the word)</p></li><li class="listitem"><p>Substitution: <code class="literal">'chat CONGRATULATIONS,
    '</code> (without the quotes, but note the space after the
    word)</p></li></ul></div></div></div><br class="example-break"/><p>Whenever you enter a command that starts with
<strong class="userinput"><code>gra</code></strong> followed by a space, that part
(<strong class="userinput"><code>gra</code></strong> and the space) is replaced by the given
pattern. So if you enter <strong class="userinput"><code>gra Bob</code></strong>, what will be
sent to the MUD is <strong class="userinput"><code>chat CONGRATULATIONS,
Bob</code></strong>.</p><p>Since aliases are just a substitution, you can define bracketed
expressions in the pattern and then use <code class="literal">$1</code>,
<code class="literal">$2</code>, and so on in the substitution. Let us increment
the above example to add something to the end. We will need a
bracketed expression:</p><div class="example" id="idm1669"><div class="example-title">Example 9.3. An alias that uses bracketed expressions</div><div class="example-contents"><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>Pattern: <code class="literal">^gra (.*)$</code></p></li><li class="listitem"><p>Substitution: <code class="literal">chat CONGRATULATIONS, $1!!!</code></p></li></ul></div></div></div><br class="example-break"/><p>It should be easy to understand. <code class="literal">$1</code> in the
substituion is replaced by what goes after
<strong class="userinput"><code>gra</code></strong>.</p><section class="sect2" id="idm1682"><div class="titlepage"><div><div><h3 class="title">9.2.1. Advanced Features</h3></div></div></div><p>As mentioned in <a class="xref" href="ch09s01.xhtml#sec_add_alias_gui" title="9.1.1. Adding Aliases">Section 9.1.1, “Adding Aliases”</a>, aliases
can use a <code class="literal">s//e</code> construct, that is, whose
substitution is actually composed of Perl statements evaluated when a
match is found.</p><p>To use that feature, select the <span class="guilabel">Eval substitution as
Perl statement</span> option:</p><div class="example" id="ex_perleval_alias"><div class="example-title">Example 9.4. An alias whose substitution is evaluated:</div><div class="example-contents"><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>Pattern: <code class="literal">calc\((.*)\)</code></p></li><li class="listitem"><p>Substitution: <code class="literal">eval "$1"</code></p></li><li class="listitem"><p>Eval substitution as Perl statement: checked</p></li></ul></div></div></div><br class="example-break"/><p>The alias allows you to write something like that:
<strong class="userinput"><code>gossip 2+3 = calc(2+3)</code></strong> and have the result of
the calculation sent to the MUD. (Or any other Perl statement to be
evaluated, actually.)</p><p>By default, case is considered when matching. If you want
case-insensitive matching, select <span class="guilabel">Ignore case when
matching</span>.</p></section><section class="sect2" id="idm1706"><div class="titlepage"><div><div><h3 class="title">9.2.2. Using Perl in Aliases</h3></div></div></div><p>It is possible to make an alias whose action is not to send a
changed command line, but to execute an action.</p><p>To do that, make the replacement start with
<code class="literal">/</code>, 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 <code class="literal">$</code> prefix by writing it as
<code class="literal">\$</code>. This way, when the substitution is run, you get
the name of the variable, not the value this variable currently
has.</p><p>As an example, if you define an alias with pattern
<strong class="userinput"><code>^say (.*)$</code></strong> and action
<strong class="userinput"><code>/\$world-\&gt;echonl("$1")</code></strong>, whenever you type a
line that starts with <strong class="userinput"><code>say</code></strong> in the input box, the
rest of the line will be echoed in the screen, but not sent to the
mud.</p></section></section><footer><div class="navfooter"><hr/><table style="width: 100%; "><tr><td style="width: 40%; text-align: left; "><a accesskey="p" href="ch09s01.xhtml">Prev</a> </td><td style="width: 20%; text-align: center; "><a accesskey="u" href="chap_aliases.xhtml">Up</a></td><td style="width: 40%; text-align: right; "> <a accesskey="n" href="sec_alias_cmdline.xhtml">Next</a></td></tr><tr><td style="width: 40%; text-align: left; vertical-align: top; ">9.1. Creating and Editing Aliases </td><td style="width: 20%; text-align: center; "><a accesskey="h" href="index.xhtml">Home</a></td><td style="width: 40%; text-align: right; vertical-align: top; "> 9.3. Defining Aliases in the Command Line</td></tr></table></div></footer></body></html>