FVWMPERL(1)

NAME
FvwmPerl - the FVWM perl manipulator

SYNOPSIS
FvwmPerl is spawned by fvwm(1), so no command line invocation will work.

DESCRIPTION
This module is intended to extend fvwm commands with the perl scripting power. It enables to embed perl expressions in the fvwmrc files and construct fvwm commands.

INVOCATION
FvwmPerl may be invoked from a fvwmrc file. If you want to invoke the unique and persistent instanse of FvwmPerl, it is suggested to do this from the StartFunction. Calling it from the top is also possible, but has several issues not discussed here.

    AddToFunc StartFunction I Module FvwmPerl
There are several command line switches:

FvwmPerl [ --eval line ] [ --load file ] [ --preprocess [ --quote char ] [ --command ] [ --nosend ] [ --noremove ] [ line | file ] ] [ --stay ] [ --nolock ] [ alias ]

Long switches may be abbreviated to short one-letter switches.

-e|--eval line - evaluate the given perl code

-l|--load file - evaluate perl code in the given file

-p|--preprocess file - preprocess the given fvwmrc file

-c|--command line - an FVWM command to be preprocessed instead of file

-q|--quote char - changes the default '%' quote

--nosend - do not send the preprocessed file to fvwm for Reading, the default is send. Useful for preprocessing non fvwmrc files.

--noremove - do not remove the preprocessed file after sending it to fvwm for Reading, the default is remove. Useful for debugging.

-s|--stay - continues an execution after --eval, --load or --preprocess are processed. By default, the module is not persistent in this case, i.e. --nostay is assumed.

--nolock - when one of the 3 action options is given, this option causes unlocking fvwm immediately. By default the requested action is executed synchronously; this only makes difference when invoked like:

  ModuleSynchronous FvwmPerl --preprocess someconfig.ppp
If --nolock is added here, ModuleSynchronous returns immediately. Note that Module returns immediately regardless of this option.

ACTIONS
There are several actions that FvwmPerl may perform: Evaluate a line of perl code.

A special function command("command") may be used in perl code to send commands back to fvwm.

If perl code contains an error, it is printed to the standard error stream with the [FvwmPerl][eval]: header prepended. Load a file of perl code. If the file is not fully qualified, it is searched in the user directory $FVWM_USERDIR (usually ~/.fvwm) and the system wide data directory $FVWM_DATADIR.

A special function command("command") may be used in perl code to send commands back to fvwm.

If perl code contains an error, it is printed to the standard error stream with the [FvwmPerl][load]: header prepended. Preprocess fvwmrc file or (if --command is given) line. This file contains lines that are not touched (usually FVWM commands) and specially preformatted perl code that is processed and replaced. Text enclosed in %{ ... }% delimiters, that may start anywhere on the line and end anywhere on the same or an other line, is perl code.

The quote parameter changes perl code delimiters. If a single char is given, like '@', the delimiters are @{ ... }@. If the given quote is 2 chars, like <>, the quotes are <{ ... }>

The perl code is substituted for the result of its evaluation. I.e. %{$a = "c"; ++$a}% is replaced with "d".

The evaluation is unlike eval and load is done under the package PreprocessNamespace and without use strict, so you are free to use any variable names without fear of conflicts. Just don't use uninitialized variables to mean undef or empty list (they may be in fact initialized by the previous preprocess action), and do a clean-up if needed. The variables and function in the main package are still available, like ::command() or ::skip(), but it is just not a good idea to access them while preprocessing.

There is a special function include(file) that loads a file, preprocesses it and returns the preprocessed result. Avoid recursion.

If any embedded perl code contains an error, it is printed to the standard error stream and prepended with the [FvwmPerl][preprocess]: header. The result of substitution is empty in this case.

The following variables may be used in the perl code:

$USER, $DISPLAY, $WIDTH, $HEIGHT, $FVWM_VERSION, $FVWM_MODULEDIR, $FVWM_DATADIR, $FVWM_USERDIR

The following line based directives are recognized when preprocessing. They are processed after the perl code (if any) is substituted.
    Causes the following lines to be repeated count times. Causes the following lines to be interpreted as the given module configuration. If "destroy" is specified the previous module configuration is destroyed first. Prefixes the following lines with the quoted prefix. Ends any of the directives described above, may be nested.

    Examples:

      %Prefix "AddToFunc SwitchToWindow I"
        Iconify off
        WindowShade off
        Raise
        WrapToWindow 50 50
      %End
      %ModuleConfig FvwmPager destroy
        Colorset 0
        Font lucidasans-10
        DeskTopScale 28
        MiniIcons
      %End ModuleConfig FvwmPager
      %Prefix "All (MyWindowToAnimate) ResizeMove "
      100 100 %{($WIDTH - 100) / 2}% %{($HEIGHT - 100) / 2}%
      %Repeat %{$count}%
      br w+2c w+2c w-1c w-1c
      %End
      %Repeat %{$count}%
      br w-2c w-2c w+1c w+1c
      %End
      %End Prefix
    Additional preprocess parameters --nosend and --noremove may be given too. See their description at the top.

These 3 actions may be requested in one of 3 ways: 1) in the command line when FvwmPerl is invoked (in this case FvwmPerl is short-lived unless --stay is also given), 2) by sending the corresponding message in fvwmrc using SendToModule, 3) by calling the corresponding perl function in perl code.

FUNCTIONS
There are several functions that perl code may call: In case of eval or load - send back to fvwm a string $fvwmCommand. In case of preprocess - append a string $fvwmCommand to the output of the embedded perl code. This function is equivalent to the eval functionality on the string $perlCode, described above. This function is equivalent to the load functionality on the file $fileName, described above. This function is equivalent to the preprocess functionality with the given parameters and the file $fileName described above.

VARIABLES
There are several global variables in the main namespace that may be used in the perl code:

    $a, $b, ... $h
    @a, @b, ... @h
    %a, %b, ... %h
They all are initialized to the empty value and may be used to store a state between different calls to FvwmPerl actions (eval and load).

If you need more readable variable names, either write "no strict 'vars';" at the start of every perl code or use a hash for this, like:

    $h{id} = $h{firstName} . " " . $h{secondName}
or use a package name, like:

    @MyMenu::terminals = qw( xterm rxvt );
    $MyMenu::itemNum = @MyMenu::terminals;
There may be a configuration option to turn strictness on and off.

MESSAGES
FvwmPerl may receive messages using the fvwm command SendToModule. The names, meanings and parameters of the messages are the same as the corresponding actions, described above.

Additionally, a message stop causes a module to quit. A message dump dumps the contents of the changed variables.

EXAMPLES
A simple test:

    SendToModule FvwmPerl eval $h{dir} = $ENV{HOME}
    SendToModule FvwmPerl eval load($h{dir} . "/test.fpl")
    SendToModule FvwmPerl load $[HOME]/test.fpl
    SendToModule FvwmPerl preprocess fvwmrc.ppp
    SendToModule FvwmPerl eval dump(\%h, @a);
    SendToModule FvwmPerl stop
The following example handles root backgrounds in fvwmrc. All these commands may be added to StartFunction.

    Module FvwmPerl
    # find all background pixmaps for a later use
    SendToModule FvwmPerl eval $a = $ENV{HOME} . "/bg"; \
      opendir DIR, $a; @b = grep { /xpm$/ } readdir(DIR); closedir DIR
    # build a menu of background pixmaps
    AddToMenu MyBackgrounds "My Backgrounds" Title
    SendToModule FvwmPerl eval foreach $b (@b) \
      { command("AddToMenu MyBackgrounds '$b' Exec fvwm-root $a/$b") }
    # choose a random background to load on start-up
    SendToModule FvwmPerl eval command("AddToFunc \
      InitFunction + I Exec exec fvwm-root $a/" . $b[int(random(@b))])

ESCAPING
SendToModule just like any other fvwm commands expands several dollar prefixed variables. This may clash with the dollars perl uses. You may avoid this by prefixing SendToModule with a leading dash. The following 2 lines in each pair are equivalent:

    SendToModule FvwmPerl eval $$d = "$[DISPLAY]"
    -SendToModule FvwmPerl eval $d = "$ENV{DISPLAY}"
    SendToModule FvwmPerl eval \
        command("Echo desk=$d, display=$$d")
    SendToModule FvwmPerl preprocess -c \
        Echo desk=%("$d")%, display=%{$$d}%
Another solution to avoid escaping of special symbols like dollars and backslashes is to create a perl file in ~/.fvwm and then load it:

    SendToModule FvwmPerl load build-menus.fpl
If you need to preprocess one command starting with a dash, you should precede it using "--".

    # this prints the current desk, i.e. "0"
    SendToModule FvwmPerl preprocess -c Echo "$%{$a = "c"; ++$a}%"
    # this prints "$d"
    SendToModule FvwmPerl preprocess -c -- -Echo "$%{"d"}%"
    # this prints "$d" (SendToModule expands $$ to $)
    SendToModule FvwmPerl preprocess -c -- -Echo "$$%{"d"}%"
    # this prints "$$d"
    -SendToModule FvwmPerl preprocess -c -- -Echo "$$%{"d"}%"
Again, it is suggested to put your command(s) into file and preprocess the file instead.

CAVEATS
FvwmPerl being written in perl and dealing with perl, follows the famous perl motto: "There's more than one way to do it", so the choice is yours.

Here are more pairs of equivalent lines:

    Module FvwmPerl --load "my.fpl" --stay
    Module FvwmPerl -e 'load("my.fpl")' -s
    SendToModule FvwmPerl preprocess --quote '@' my.ppp
    SendToModule FvwmPerl eval preprocess({quote => '@'}, "my.ppp");
Warning, you may affect the way FvwmPerl works by evaluating appropriate perl code, this is considered a feature not a bug. But please don't do this, write your own FVWM module in perl instead.

SEE ALSO
The fvwm(1) man page describes all available commands.

Basically, in your perl code you may use any function or class method from the perl library installed with FVWM, see the man pages of perl packages General::FileSystem, General::Parse and FVWM::Module.

AUTHOR
Mikhael Goikhman <migo@homemail.com>.