Hi everybody. I'm taking the time to configure an Awesome WM after completing my first (and lengthy) Gentoo install. The Awesome config files are all in Lua and I figured automating the process a bit would be a nice opportunity to practice Perl by parsing and modifying the config code using regex's. The code I've got so far works, but I'm hoping that the monks will look my regex's over and provide more graceful/elegant/stable variations of the same purpose for me to compare/contrast and learn from.
I used the following code to find the terminal, and taglist specified in the configuration code. These lines appear in the configuration file as:
-- This is used later as the default terminal and editor to run.
terminal = "urxvt"
-- Define a tag table which hold all screen tags.
tags = {
names = { "Main", "WWW", "GIMP", "EMail", 6, 7 },
The applicable parts of my solution:
#!/usr/bin/perl
# The configuration file is only about 400 lines,
# so I Tie::file to @config_file
my @config_file;
sub procTerm {
my $change = $_[0];
my $conf_file = $_[1];
foreach my $line (@$conf_file) {
if ($line =~ m/^\s*terminal\s=\s"([^"]+)"\s*$/) {
$line =~ s/$1/$change/;
}
}
}
sub changeTerm {
my $conf_file = shift;
print "Change Terminal: ";
chomp (my $newTerm = <STDIN>);
procTerm($newTerm, $conf_file);
}
sub getTags {
my $conf_file = shift;
my @tags;
foreach my $line (@$conf_file) {
if ($line =~ m/^\s*names\s=\s{(\s*(.+),?)+\s*/) {
@tags = split ",", $line;
@tags = map {
if ($_ =~ m/"([^"]+)"/) {$_ = $1}
elsif ($_ =~ m/([^\s]+)/) {$_ = $1}
} @tags;
}
}
print Dumper \@tags;
}
changeTerm(\@config_file);
getTags(\@config_file);
I don't feel like my regex's are as efficient as they could be, but these are the best that I was able to work up. I understand that this is a bit like the frequently discouraged reinvention of the wheel. A CPAN search provided the following:
I plan on using these Modules to make my own frontend for manipulating Awesome, but I have really been looking for interesting opportunities to practice the use of regex's.
I appreciate any input on my coding, and offerings of alternate solutions. Thanks guys.
**To any rookie programmer/self-starters like myself: If you find yourself looking for projects to get practice, and stay motivated I would definitely suggest one of the more difficult *nix installs. The installation/configuration process definitely opened several paths of inspiration that feel more useful, and are more fun than book exercises. Not to mention I learned a TON about *nix **
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.