Mercurial > pidgin.yaz
comparison doc/PERL-HOWTO.dox @ 6602:76683fe3c96d
[gaim-migrate @ 7126]
Cleaned up the page a bit. It's better organized now, and uses doxygen's
section stuff.
committer: Tailor Script <tailor@pidgin.im>
author | Christian Hammond <chipx86@chipx86.com> |
---|---|
date | Sun, 24 Aug 2003 05:04:08 +0000 |
parents | 2f6850c7d980 |
children | 21084026030b |
comparison
equal
deleted
inserted
replaced
6601:1521c3b63d7f | 6602:76683fe3c96d |
---|---|
1 /** @page perl-howto Perl Scripting HOWTO | 1 /** @page perl-howto Perl Scripting HOWTO |
2 | 2 |
3 <h2>Perl Scripting HOWTO</h2> | 3 @section Introduction |
4 Perl is the first scripting language compatible with Gaim, and has been a | |
5 valuable aid to developers wishing to write scripts to modify the behavior | |
6 of their favorite instant messenger. A perl script acts like a normal | |
7 plugin, and appears in the list of plugins in Gaim's preferences pane. | |
8 Until now, they have been very basic, and consisted of only a few | |
9 functions. However, with the latest changes to Gaim's perl API, a much | |
10 larger part of Gaim is now open. In time, even such things as Gtk+ | |
11 preference panes for perl scripts will be possible. | |
12 @endsection | |
13 | |
14 @section Writing your first perl script | |
15 Enough of that. You want to know how to write a perl script, right? | |
16 | |
17 First off, we're going to assume here that you are familiar with perl. Perl | |
18 scripts in Gaim are just normal perl scripts, which happen to use Gaim's | |
19 loadable perl module. | |
20 | |
21 Now, you're going to want to place your script in $HOME/.gaim/plugins/. | |
22 That's the place where all plugins and scripts go, regardless of language. | |
23 | |
24 The first thing you're going to write in your script is the necessary code | |
25 to actually register and initialize your script, which looks something like | |
26 this: | |
4 | 27 |
5 | 28 |
6 <h3>Introduction</h3> | 29 @code |
30 use Gaim; | |
7 | 31 |
8 Perl is the first scripting language compatible with Gaim, and has been a | 32 %PLUGIN_INFO = ( |
9 valuable aid to developers wishing to write scripts to modify the behavior | 33 perl_api_version => 2, |
10 of their favorite instant messenger. A perl script acts like a normal | 34 name => "Your Plugin's Name", |
11 plugin, and appears in the list of plugins in Gaim's preferences pane. | 35 version => "0.1", |
12 Until now, they have been very basic, and consisted of only a few | 36 summary => "Brief summary of your plugin.", |
13 functions. However, with the latest changes to Gaim's perl API, a much | 37 description => "Detailed description of what your plugin does.", |
14 larger part of Gaim is now open. In time, even such things as Gtk+ | 38 author => "Your Name <email@address>", |
15 preference panes for perl scripts will be possible. | 39 url => "http://yoursite.com/", |
16 | 40 |
17 <h3>Writing your first perl script</h3> | 41 load => "plugin_load", |
42 unload => "plugin_unload" | |
43 ) | |
18 | 44 |
19 Enough of that. You want to know how to write a perl script, right? | 45 sub plugin_init { |
46 return %PLUGIN_INFO; | |
47 } | |
20 | 48 |
21 First off, we're going to assume here that you are familiar with perl. Perl | 49 sub plugin_load { |
22 scripts in Gaim are just normal perl scripts, which happen to use Gaim's | 50 my $plugin = shift; |
23 loadable perl module. | 51 } |
24 | 52 |
25 Now, you're going to want to place your script in $HOME/.gaim/plugins/. | 53 sub plugin_unload { |
26 That's the place where all plugins and scripts go, regardless of language. | 54 my $plugin = shift; |
27 | 55 } |
28 The first thing you're going to write in your script is the necessary code | 56 @endcode |
29 to actually register and initialize your script, which looks something like | |
30 this: | |
31 | 57 |
32 | 58 |
33 @code | 59 The first thing you see is a hash called @c @%PLUGIN_INFO. It contains |
34 use Gaim; | 60 the basic information on your plugin. In the future, additional fields |
61 may be allowed, such as ones to setup a Gtk+ preferences pane. | |
35 | 62 |
36 %PLUGIN_INFO = ( | 63 The @c plugin_init function is required in all perl scripts. Its job |
37 perl_api_version => 2, | 64 is to return the @c @%PLUGIN_INFO hash, which Gaim will use to register |
38 name => "Your Plugin's Name", | 65 and initialize your plugin. |
39 version => "0.1", | |
40 summary => "Brief summary of your plugin.", | |
41 description => "Detailed description of what your plugin does.", | |
42 author => "Your Name <email@address>", | |
43 url => "http://yoursite.com/", | |
44 | 66 |
45 load => "plugin_load", | 67 The @c plugin_load function is called when the user loads your plugin |
46 unload => "plugin_unload" | 68 from the preferences, or on startup. It is passed one variable, which |
47 ) | 69 is a handle to the plugin. This must be used when registering signal |
70 handlers or timers. | |
48 | 71 |
49 sub plugin_init { | 72 The @c plugin_unload function is called when the user is unloading your |
50 return %PLUGIN_INFO; | 73 plugin. Its job is to clean up anything that must be dealt with before |
51 } | 74 unloading, such as removing temporary files or saving configuration |
75 information. It does @em not have to unregister signal handlers or timers, | |
76 as Gaim will do that for you. | |
52 | 77 |
53 sub plugin_load { | 78 @warning Do @b NOT put any executable code outside of these functions |
54 my $plugin = shift; | 79 or your own user-defined functions. Variable declarations are |
55 } | 80 okay, as long as they're set to be local. Bad Things (TM) can |
81 happen if you don't follow this simple instruction. | |
56 | 82 |
57 sub plugin_unload { | 83 @endsection |
58 my $plugin = shift; | 84 |
59 } | 85 @section Timeouts |
60 @endcode | 86 One feature useful to many perl plugin writers are timeouts. Timeouts allow |
87 code to be ran after a specified number of seconds, and are rather | |
88 simple to setup. Here's one way of registering a timeout. | |
61 | 89 |
62 | 90 |
63 The first thing you see is a hash called @c @%PLUGIN_INFO. It contains | 91 @code |
64 the basic information on your plugin. In the future, additional fields | 92 sub timeout_cb { |
65 may be allowed, such as ones to setup a Gtk+ preferences pane. | 93 my $data = shift; |
66 | 94 |
67 The @c plugin_init function is required in all perl scripts. Its job | 95 Gaim::debug_info("my perl plugin", |
68 is to return the @c @%PLUGIN_INFO hash, which Gaim will use to register | 96 "Timeout callback called! data says: $data\n"); |
69 and initialize your plugin. | 97 } |
70 | 98 |
71 The @c plugin_load function is called when the user loads your plugin | 99 sub plugin_load { |
72 from the preferences, or on startup. It is passed one variable, which | 100 my $plugin = shift; |
73 is a handle to the plugin. This must be used when registering signal | |
74 handlers or timers. | |
75 | 101 |
76 The @c plugin_unload function is called when the user is unloading your | 102 # Start a timeout for 5 seconds. |
77 plugin. Its job is to clean up anything that must be dealt with before | 103 Gaim::timeout_add($plugin, 5, \&timeout_cb, "Hello!"); |
78 unloading, such as removing temporary files or saving configuration | 104 } |
79 information. It does @em not have to unregister signal handlers or timers, | 105 @endcode |
80 as Gaim will do that for you. | |
81 | |
82 @warning Do @b NOT put any executable code outside of these functions | |
83 or your own user-defined functions. Variable declarations are | |
84 okay, as long as they're set to be local. Bad Things (TM) can | |
85 happen if you don't follow this simple instruction. | |
86 | 106 |
87 | 107 |
88 <h3>Timeouts</h3> | 108 Here's another way of calling a timeout: |
89 | |
90 One feature useful to many perl plugin writers are timeouts. Timeouts allow | |
91 code to be ran after a specified number of seconds, and are rather | |
92 simple to setup. Here's one way of registering a timeout. | |
93 | 109 |
94 | 110 |
95 @code | 111 @code |
96 sub timeout_cb { | 112 sub plugin_load { |
97 my $data = shift; | 113 my $plugin = shift; |
98 | 114 |
99 Gaim::debug_info("my perl plugin", | 115 # Start a timeout for 5 seconds. |
100 "Timeout callback called! data says: $data\n"); | 116 Gaim::timeout_add($plugin, 5, |
101 } | 117 sub { |
118 my $data = shift; | |
102 | 119 |
103 sub plugin_load { | 120 Gaim::debug_info("my perl plugin", |
104 my $plugin = shift; | 121 "Timeout callback called! data says: $data\n"); |
105 | 122 }, "Hello!"); |
106 # Start a timeout for 5 seconds. | 123 } |
107 Gaim::timeout_add($plugin, 5, \&timeout_cb, "Hello!"); | |
108 } | |
109 @endcode | |
110 | |
111 | |
112 Here's another way of calling a timeout: | |
113 | |
114 | |
115 @code | |
116 sub plugin_load { | |
117 my $plugin = shift; | |
118 | |
119 # Start a timeout for 5 seconds. | |
120 Gaim::timeout_add($plugin, 5, | |
121 sub { | |
122 my $data = shift; | |
123 | |
124 Gaim::debug_info("my perl plugin", | |
125 "Timeout callback called! data says: $data\n"); | |
126 }, "Hello!"); | |
127 } | |
128 @endcode | 124 @endcode |
129 | 125 |
130 | 126 |
131 A timeout automatically unregisters when it reaches 0 (which is also | 127 A timeout automatically unregisters when it reaches 0 (which is also |
132 when the callback is called). If you want a timeout to call a function | 128 when the callback is called). If you want a timeout to call a function |
133 every specified number of seconds, just re-register the timeout | 129 every specified number of seconds, just re-register the timeout |
134 at the end of the callback. | 130 at the end of the callback. |
135 | 131 |
136 The data parameter is optional. If you don't have data to pass to the | 132 The data parameter is optional. If you don't have data to pass to the |
137 callback, simply omit the parameter. | 133 callback, simply omit the parameter. |
134 @endsection | |
138 | 135 |
139 | 136 @section Signals |
140 <h3>Signals</h3> | |
141 | |
142 Signals are how gaim plugins get events. There are a number of | 137 Signals are how gaim plugins get events. There are a number of |
143 @ref Signals signals available. | 138 @ref Signals signals available. |
144 | 139 |
145 A signal is registered by connecting a signal name owned by an | 140 A signal is registered by connecting a signal name owned by an |
146 instance handle to a callback on the plugin handle. This is best | 141 instance handle to a callback on the plugin handle. This is best |
147 illustrated with an example. | 142 illustrated with an example. |
148 | 143 |
149 | 144 |
150 @code | 145 @code |
151 sub connection_signed_on_cb { | 146 sub connection_signed_on_cb { |
152 my ($gc, $data) = @_; | 147 my ($gc, $data) = @_; |
153 my $account = $gc->get_account(); | 148 my $account = $gc->get_account(); |
154 | 149 |
155 Gaim::debug_info("my perl plugin", | 150 Gaim::debug_info("my perl plugin", |
156 "Account " . $account->get_username() . " signed on.\n"); | 151 "Account " . $account->get_username() . " signed on.\n"); |
157 } | 152 } |
153 | |
154 sub plugin_load { | |
155 my $plugin = shift; | |
156 my $data = ""; | |
158 | 157 |
159 sub plugin_load { | 158 Gaim::signal_connect(Gaim::Connections::handle, "signed-on", |
160 my $plugin = shift; | 159 $plugin, \&signed_on_cb, $data); |
161 my $data = ""; | 160 } |
162 | |
163 Gaim::signal_connect(Gaim::Connections::handle, "signed-on", | |
164 $plugin, \&signed_on_cb, $data); | |
165 } | |
166 @endcode | 161 @endcode |
167 | 162 |
168 | 163 |
169 Like timeouts, the callback can be an embedded subroutine, and also | 164 Like timeouts, the callback can be an embedded subroutine, and also |
170 like timeouts, the data parameter can be omitted. | 165 like timeouts, the data parameter can be omitted. |
166 @endsection | |
171 | 167 |
172 | 168 @section Notes |
173 <h3>Notes</h3> | |
174 | |
175 The API in perl is very similar to Gaim's C API. The functions have been | 169 The API in perl is very similar to Gaim's C API. The functions have been |
176 gathered into packages, but most are the same, and the documentation can | 170 gathered into packages, but most are the same, and the documentation can |
177 be a big help at times. | 171 be a big help at times. |
172 @endsection | |
178 | 173 |
179 | 174 @section Resources |
180 <h3>Resources</h3> | |
181 | |
182 @see Signals | 175 @see Signals |
183 @see Perl API Reference | 176 @see Perl API Reference |
177 @endsection | |
178 | |
184 */ | 179 */ |
185 // vim: syntax=c tw=75 et | 180 // vim: syntax=c tw=75 et |