Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation


by blazar (Canon)
on Dec 27, 2004 at 21:58 UTC ( #417646=sourcecode: print w/replies, xml ) Need Help??
Category: Utility Scripts
Author/Contact Info
Description: Naive Window Manager selection tool

Data used by the program is appended to it after the __END__ token. Embedded pod documentation describes its format for maintainers/admins (no perl experience/knowledge required). This is being actually used at a site: it's rudimentary but it works! Users seem to be satisfied.

Note that die()s and warn()ings intended for the final users are in "\n"-form, whereas those for maintainers are in the somewhat more informative "\n"-less one, so before pointing out there's an inconsistency consider that it's there by design...

Suggestions and improvements welcome. About the only feature I've considered adding myself is support for multi-line text to be inserted in ~/.xinitrc, but up until now nobody has requested it at least in the environment this is being used.

#!/usr/bin/perl -l


Naive program for B<Window Manager> selection: (over)writes one's
F<`~/.xinitrc'>.  Data for the program is appended literally at
the end of it, after the __END__ token and has the form

C<< <key>:<name>:<cmd> >>

where <key> is the key to be selected by the user, preferably
consisting of one or at most a few chars, <name> is the name of
the Window Manager and <cmd> is the command to be put in F<`~/.xinitrc
<key> is case-insensitive and shown uppercase.

In the unlikely event that any item contains literal C<:>'s, any longe
string of colons can be used as a separator instead, like thus:

C<L::LCM:Cool!::exec lcmwm>

Lines with no colons are silently ignored.  No additional checking is


use strict;
use warnings;

my ($q,%wm,%saw)='Q'; # Quit Key

while (<DATA>) {
    my $sep;
    $sep |= $_ for /:+/g;
    $sep or next;
    my ($k,$n,$cmd)=split /$sep/;
    $k=uc $k;
    (warn "$0: Cannot use [$k] as a key for `$n'"),
      next if $k eq $q;
    (warn "$0: Duplicate key [$k]. Skipping `$n'"),
      next if $saw{$k}++;

    print "Choose Window Manager:";
    print "[$_] $wm{$_}[0]" for sort keys %wm;
    print "[$q] <QUIT>";
    chomp(my $in=uc <STDIN>);
    last if $in eq $q;
    exists $wm{$in} or
      (warn "Invalid choice: [$in]\n"), redo;
    open my $fh, '>', "$ENV{HOME}/.xinitrc" or
      die "Can't write to ~/.xinitrc: $!\n";
    print $fh $wm{$in}[1];
    print <<"EOT";
`~/.xinitrc' successfully updated for use with $wm{$in}[0]!
Do you want to start X now? 
[y/Y] to do so, anything else to quit.
    exec 'startx' 
      if uc <STDIN> eq "Y\n";

G:Gnome:exec gnome-session
K:KDE:exec startkde
B:Blackbox:exec blackbox
E:Enlightenment:exec enlightenment
F:FVWM:exec fvwm
I:IceWM:exec icewm-session
S:Sawfish:exec sawfish
W:Window Maker:exec wmaker
A:Waimea:exec waimea
X:XFce3:exec xfwm
X4:XFce4:exec startxfce4
U:Fluxbox:exec fluxbox
L:evilwm:exec evilwm
Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://417646]
and a soft breeze sighs...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2018-06-21 03:29 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (117 votes). Check out past polls.