1FvwmPerl(1)                      Fvwm Modules                      FvwmPerl(1)
2
3
4

NAME

6       FvwmPerl - the fvwm perl manipulator and preprocessor
7

SYNOPSIS

9       FvwmPerl should be spawned by fvwm(1) for normal functionality.
10
11       To run this module, place this command somewhere in the configuration:
12
13           Module FvwmPerl [params]
14
15       or:
16
17           ModuleSynchronize FvwmPerl [params]
18
19       if you want to immediately start to send commands to FvwmPerl.
20

DESCRIPTION

22       This module is intended to extend fvwm commands with the perl scripting
23       power.  It enables to embed perl expressions in the fvwm config files
24       and construct fvwm commands.
25

INVOCATION

27       If you want to invoke the unique and persistent instance of FvwmPerl,
28       it is suggested to do this from the StartFunction.  Calling it from the
29       top is also possible, but involves some issues not discussed here.
30
31           AddToFunc StartFunction I Module FvwmPerl
32
33       There are several command line switches:
34
35       FvwmPerl [ --eval line ] [ --load file ] [ --preprocess [ --quote char
36       ] [ --winid wid ] [ --cmd ] [ --nosend ] [ --noremove ] [ line ⎪ file ]
37       ] [ --export [names] ] [ --stay ] [ --nolock ] [ alias ]
38
39       Long switches may be abbreviated to short one-letter switches.
40
41       -e--eval line - evaluate the given perl code
42
43       -l--load file - evaluate perl code in the given file
44
45       -p--preprocess [ file ] - preprocess the given fvwm config file
46
47       The following 5 options are only valid together with --preprocess
48       option.
49
50       -c--cmd line - an fvwm command to be preprocessed instead of file
51
52       -q--quote char - change the default '%' quote
53
54       -w--winid wid - set explicit window context (should begin with digit,
55       may be in oct or hex form; this window id overwrites implicit window
56       context if any)
57
58       --nosend - do not send the preprocessed file to fvwm for Reading, the
59       default is send. Useful for preprocessing non fvwm config files.
60
61       --noremove - do not remove the preprocessed file after sending it to
62       fvwm for Reading, the default is remove. Useful for debugging.
63
64       -x--export [names] - define fvwm shortcut functions (by default, two
65       functions named "Eval" and ".").  This option implies --stay.
66
67       -s--stay - continues an execution after --eval, --load or --preprocess
68       are processed.  By default, the module is not persistent in this case,
69       i.e. --nostay is assumed.
70
71       --nolock - when one of the 3 action options is given, this option
72       causes unlocking fvwm immediately. By default the requested action is
73       executed synchronously; this only makes difference when invoked like:
74
75           ModuleSynchronous FvwmPerl --preprocess someconfig.ppp
76
77       If --nolock is added here, ModuleSynchronous returns immediately.  Note
78       that Module returns immediately regardless of this option.
79

USING ALIAS

81       Aliases allow to have several module invocations and work separately
82       with all invocations, here is an example:
83
84           ModuleSynchronous FvwmPerl FvwmPerl-JustTest
85           SendToModule FvwmPerl-JustTest eval $a = 2 + 2; $b = $a
86           SendToModule FvwmPerl-JustTest eval cmd("Echo 2 + 2 = $b")
87           KillModule FvwmPerl FvwmPerl-JustTest
88

PREPROCESSING EXAMPLE

90       One of the effective proprocessing solutions is to pass the whole fvwm
91       configuration with embeded perl code to "FvwmPerl --preprocess".  An
92       alternative approach is to write a perl script that produces fvwm com‐
93       mands and sends them for execution, this script may be loaded using
94       "FvwmPerl --load". There are hovewer intermediate solutions that pre‐
95       process only separate configuration lines (or alternatively, execute
96       separate perl commands that produce fvwm commands).
97
98       The following code snippet adds ability of arithmetics and string
99       scripting to certain lines that need this. To use this, you want to
100       start FvwmPerl as your first command so that other commands may be
101       asked to be preprosessed.
102
103           ModuleSynchronize FvwmPerl
104
105           AddToFunc .
106           + I SendToModule FvwmPerl preprocess -c -- $*
107
108           . Exec exec xterm -name xterm-%{++$i}%   # use unique name
109
110           . GotoDesk 0 %{ $[desk.n] + 1 }%         # go to next desk
111
112           . Exec exec %{ -x "/usr/bin/X11/aterm" ? "aterm" : "xterm" }% -sb
113
114           # center a window
115           Next (MyWindow) . Move \
116             %{($WIDTH - $[w.width]) / 2}%p %{($HEIGHT - $[w.height]) / 2}%p
117
118           . Exec exec xmessage %{2 + 2}%           # simple calculator
119
120           . %{main::showMessage(2 + 2, "Yet another Calculator"); ""}%
121

ACTIONS

123       There are several actions that FvwmPerl may perform:
124
125       eval perl-code
126           Evaluate a line of perl code.
127
128           A special function cmd("command") may be used in perl code to send
129           commands back to fvwm.
130
131           If perl code contains an error, it is printed to the standard error
132           stream with the [FvwmPerl][eval]: header prepended.
133
134       load file-name
135           Load a file of perl code.  If the file is not fully qualified, it
136           is searched in the user directory $FVWM_USERDIR (usually ~/.fvwm)
137           and the system wide data directory $FVWM_DATADIR.
138
139           A special function cmd("command") may be used in perl code to send
140           commands back to fvwm.
141
142           If perl code contains an error, it is printed to the standard error
143           stream with the [FvwmPerl][load]: header prepended.
144
145       preprocess [-q⎪--quote char] [-c⎪--cmd] [linefile]
146           Preprocess fvwm config file or (if --cmd is given) line.  This file
147           contains lines that are not touched (usually fvwm commands) and
148           specially preformatted perl code that is processed and replaced.
149           Text enclosed in %{ ... }% delimiters, that may start anywhere on
150           the line and end anywhere on the same or an other line, is perl
151           code.
152
153           The quote parameter changes perl code delimiters.  If a single char
154           is given, like '@', the delimiters are @{ ... }@.  If the given
155           quote is 2 chars, like <>, the quotes are <{ ... }>
156
157           The perl code is substituted for the result of its evaluation.
158           I.e. %{$a = "c"; ++$a}% is replaced with "d".
159
160           The evaluation is unlike eval and load is done under the package
161           PreprocessNamespace and without use strict, so you are free to use
162           any variable names without fear of conflicts. Just don't use unini‐
163           tialized variables to mean undef or empty list (they may be in fact
164           initialized by the previous preprocess action), and do a clean-up
165           if needed.  The variables and function in the main package are
166           still available, like ::cmd() or ::skip(), but it is just not a
167           good idea to access them while preprocessing.
168
169           There is a special function include(file) that loads a file, pre‐
170           processes it and returns the preprocessed result. Avoid recursion.
171
172           If any embedded perl code contains an error, it is printed to the
173           standard error stream and prepended with the [FvwmPerl][prepro‐
174           cess]: header.  The result of substitution is empty in this case.
175
176           The following variables may be used in the perl code:
177
178           $USER, $DISPLAY, $WIDTH, $HEIGHT, $FVWM_VERSION, $FVWM_MODULEDIR,
179           $FVWM_DATADIR, $FVWM_USERDIR
180
181           The following line based directives are recognized when preprocess‐
182           ing.  They are processed after the perl code (if any) is substi‐
183           tuted.
184
185           %Repeat count
186               Causes the following lines to be repeated count times.
187
188           %ModuleConfig module-name [ destroy ]
189               Causes the following lines to be interpreted as the given mod‐
190               ule configuration.  If "destroy" is specified the previous mod‐
191               ule configuration is destroyed first.
192
193           %Prefix prefix
194               Prefixes the following lines with the quoted prefix.
195
196           %End any-optional-comment
197               Ends any of the directives described above, may be nested.
198
199           Examples:
200
201               %Prefix "AddToFunc SwitchToWindow I"
202                   Iconify off
203                   WindowShade off
204                   Raise
205                   WrapToWindow 50 50
206               %End
207
208               %ModuleConfig FvwmPager destroy
209                   Colorset 0
210                   Font lucidasans-10
211                   DeskTopScale 28
212                   MiniIcons
213               %End ModuleConfig FvwmPager
214
215               %Prefix "All (MyWindowToAnimate) ResizeMove "
216               100 100 %{($WIDTH - 100) / 2}% %{($HEIGHT - 100) / 2}%
217               %Repeat %{$count}%
218               br w+2c w+2c w-1c w-1c
219               %End
220               %Repeat %{$count}%
221               br w-2c w-2c w+1c w+1c
222               %End
223               %End Prefix
224
225           Additional preprocess parameters --nosend and --noremove may be
226           given too.  See their description at the top.
227
228       export [func-names]
229           Send to fvwm the definition of shortcut functions that help to
230           activate different actions of the module (i.e. eval, load and pre‐
231           process).
232
233           Function names (func-names) may be separated by commas or/and
234           whitespace.  By default, two functions "Eval" and "." are assumed.
235
236           The actual action defined in a function is guessed from the func‐
237           tion name if possible, where function name "." is reserved for pre‐
238           process action.
239
240           For example, any of these two fvwm commands
241
242              SendToModule MyPerl export PerlEval,PP
243              FvwmPerl --export PerlEval,PP MyPerl
244
245           define the following two shortcut functions:
246
247             DestroyFunc PerlEval
248             AddToFunc I SendToModule MyPerl eval $*
249             DestroyFunc PP
250             AddToFunc I SendToModule MyPerl preprocess -c -- $*
251
252       These 4 actions may be requested in one of 3 ways: 1) in the command
253       line when FvwmPerl is invoked (in this case FvwmPerl is short-lived
254       unless --stay or --export is also given), 2) by sending the correspond‐
255       ing message in fvwm config using SendToModule, 3) by calling the corre‐
256       sponding perl function in perl code.
257

FUNCTIONS

259       There are several functions that perl code may call:
260
261       cmd($fvwmCommand)
262           In case of eval or load - send back to fvwm a string $fvwmCommand.
263           In case of preprocess - append a string $fvwmCommand to the output
264           of the embedded perl code.
265
266       doEval($perlCode)
267           This function is equivalent to the eval functionality on the string
268           $perlCode, described above.
269
270       load($fileName)
271           This function is equivalent to the load functionality on the file
272           $fileName, described above.
273
274       preprocess(@params, ["-c $command"] [$fileName])
275           This function is equivalent to the preprocess functionality with
276           the given parameters and the file $fileName described above.
277
278       export($funcNames, [$doUnexport])
279           This function is equivalent to the export functionality with the
280           given $funcNames, described above. May also unexport the function
281           names if the second parameter is true.
282
283           Function names should be separated by commas or/and whitespace.  If
284           $funcNames is empty then functions "Eval" and "." are assumed.
285
286       stop()
287           Terminates the module.
288
289       skip()
290           Skips the rest of the event callback code, i.e. the module returns
291           to listen to new module events.
292
293       unlock()
294           Unsynchronizes the event callback from fvwm. This may be useful to
295           prevent deadlocks, i.e. usually fvwm kills the non-responding mod‐
296           ule if the event callback is not finished in ModuleTimeout seconds.
297           This prevents it.
298
299           This example causes FvwmPerl to suspend its execution for one
300           minute:
301
302               SendModule FvwmPerl eval unlock(); sleep(60);
303
304           However, verify that there is no way a new message is sent by fvwm
305           while the module is busy, and fvwm stays locked on this new message
306           for too long.  See also the detach solution if you need long last‐
307           ing operations.
308
309       detach()
310           Forks and detaches the rest of the event callback code from the
311           main process. This may be useful to prevent killing the module if
312           its event callback should take a long time to complete and it may
313           be done in the detached child. The detached child may still send
314           commands to fvwm (don't rely on this), but not receive the events
315           of course, it exits immediately after the callback execution is
316           finished.
317
318           If you use detach(), better only send commands to fvwm in one
319           process (the main one or the detached one), doing otherwise may
320           often cause conflicts.
321
322       showMessage($msg, $title[, $useStderrToo=1])
323           Shows a dialog window with the given message, using whichever tool
324           is find in the system.
325
326           See FVWM::Module::Toolkit::showMessage for more information.
327

VARIABLES

329       There are several global variables in the main namespace that may be
330       used in the perl code:
331
332           $a, $b, ... $h
333           @a, @b, ... @h
334           %a, %b, ... %h
335
336       They all are initialized to the empty value and may be used to store a
337       state between different calls to FvwmPerl actions (eval and load).
338
339       If you need more readable variable names, either write "no strict
340       'vars';" at the start of every perl code or use a hash for this, like:
341
342           $h{id} = $h{firstName} . " " . $h{secondName}
343
344       or use a package name, like:
345
346           @MyMenu::terminals = qw( xterm rxvt );
347           $MyMenu::itemNum = @MyMenu::terminals;
348
349       There may be a configuration option to turn strictness on and off.
350

MESSAGES

352       FvwmPerl may receive messages using the fvwm command SendToModule.  The
353       names, meanings and parameters of the messages are the same as the cor‐
354       responding actions, described above.
355
356       Additionally, a message stop causes a module to quit.
357
358       A message unexport [func-names] undoes the effect of export, described
359       in the ACTIONS section.
360
361       A message dump dumps the contents of the changed variables (not yet).
362

EXAMPLES

364       A simple test:
365
366           SendToModule FvwmPerl eval $h{dir} = $ENV{HOME}
367           SendToModule FvwmPerl eval load($h{dir} . "/test.fpl")
368           SendToModule FvwmPerl load $[HOME]/test.fpl
369           SendToModule FvwmPerl preprocess config.ppp
370           SendToModule FvwmPerl export Eval,PerlEval,PerlLoad,PerlPP
371           SendToModule FvwmPerl unexport PerlEval,PerlLoad,PerlPP
372           SendToModule FvwmPerl stop
373
374       The following example handles root backgrounds in fvwmrc.  All these
375       commands may be added to StartFunction.
376
377           Module FvwmPerl --export PerlEval
378
379           # find all background pixmaps for a later use
380           PerlEval $a = $ENV{HOME} . "/bg"; \
381             opendir DIR, $a; @b = grep { /xpm$/ } readdir(DIR); closedir DIR
382
383           # build a menu of background pixmaps
384           AddToMenu MyBackgrounds "My Backgrounds" Title
385           PerlEval foreach $b (@b) \
386             { cmd("AddToMenu MyBackgrounds '$b' Exec fvwm-root $a/$b") }
387
388           # choose a random background to load on start-up
389           PerlEval cmd("AddToFunc \
390             InitFunction + I Exec exec fvwm-root $a/" . $b[int(random(@b))])
391

ESCAPING

393       SendToModule just like any other fvwm commands expands several dollar
394       prefixed variables.  This may clash with the dollars perl uses.  You
395       may avoid this by prefixing SendToModule with a leading dash.  The fol‐
396       lowing 2 lines in each pair are equivalent:
397
398           SendToModule FvwmPerl eval $$d = "$[DISPLAY]"
399           -SendToModule FvwmPerl eval $d = "$ENV{DISPLAY}"
400
401           SendToModule FvwmPerl eval \
402               cmd("Echo desk=$d, display=$$d")
403           SendToModule FvwmPerl preprocess -c \
404               Echo desk=%("$d")%, display=%{$$d}%
405
406       Another solution to avoid escaping of special symbols like dollars and
407       backslashes is to create a perl file in ~/.fvwm and then load it:
408
409           SendToModule FvwmPerl load build-menus.fpl
410
411       If you need to preprocess one command starting with a dash, you should
412       precede it using "--".
413
414           # this prints the current desk, i.e. "0"
415           SendToModule FvwmPerl preprocess -c Echo "$%{$a = "c"; ++$a}%"
416           # this prints "$d"
417           SendToModule FvwmPerl preprocess -c -- -Echo "$%{"d"}%"
418           # this prints "$d" (SendToModule expands $$ to $)
419           SendToModule FvwmPerl preprocess -c -- -Echo "$$%{"d"}%"
420           # this prints "$$d"
421           -SendToModule FvwmPerl preprocess -c -- -Echo "$$%{"d"}%"
422
423       Again, it is suggested to put your command(s) into file and preprocess
424       the file instead.
425

CAVEATS

427       FvwmPerl being written in perl and dealing with perl, follows the
428       famous perl motto: "There's more than one way to do it", so the choice
429       is yours.
430
431       Here are more pairs of equivalent lines:
432
433           Module FvwmPerl --load "my.fpl" --stay
434           Module FvwmPerl -e 'load("my.fpl")' -s
435
436           SendToModule FvwmPerl preprocess --quote '@' my.ppp
437           SendToModule FvwmPerl eval preprocess({quote => '@'}, "my.ppp");
438
439       Warning, you may affect the way FvwmPerl works by evaluating appropri‐
440       ate perl code, this is considered a feature not a bug.  But please
441       don't do this, write your own fvwm module in perl instead.
442

SEE ALSO

444       The fvwm(1) man page describes all available commands.
445
446       Basically, in your perl code you may use any function or class method
447       from the perl library installed with fvwm, see the man pages of perl
448       packages General::FileSystem, General::Parse and FVWM::Module.
449

AUTHOR

451       Mikhael Goikhman <migo@homemail.com>.
452
453
454
455perl v5.8.6                       2005-08-11                       FvwmPerl(1)
Impressum