http://www.perlmonks.org?node_id=1001461

marquezc329 has asked for the wisdom of the Perl Monks concerning the following question:

Hello again everybody, I'm currently working on a project parsing Awesome Window Manager's Lua configuration files for important configuration and theme settings, displaying the desired settings, and eventually allowing the user to modify settings by editing the files.

I've been able to gather the settings and place them in hashes based on the type of setting. Now I am trying to allow the user to pick a single group or a group of groups based on their input. I plan on giving viewTheme() the ability to split comma separated input and return the split elements in the same manner as I've returned all the groups. It may sound silly, but these are the most complex data structures I've dealt with thus far in my learning and I feel like my functions are starting to get bloated (printSettings in particular) and confusing.

The code works, but I'd like to clean these functions up before adding to them. Is the bloated feeling due to my being unaccustomed to seeing code with these more complex data structures, or is my inexperience in writing code at this level causing me to write bloated inefficient code? Is this code a viable solution for what I am trying to accomplish at this point, or can you suggest changes I should make before moving forward?

Applicable functions:

sub printSettings { my @groups = shift; foreach my $table (@groups) { foreach my $info (@$table) { my $cnt = 1; print "=" x 60, "\n"; print " $info->[0]", " " x 20, "\n"; print "=" x 60, "\n"; foreach my $key (sort keys %{$info->[1]}) { printf "%5s. %-25s %-5s $info->[1]{$key}\n", $cnt, $ +key, "=>"; $cnt++; } } } } sub viewTheme { #print "[(a)ll, (b)ase, (m)enu, tag(l)ist, (o)other]{Enter to quit +}: "; #chomp (my $item = <STDIN>); my $item = shift; my $base = ["Base Elements", \%theme_base]; my $taglist = ["Taglist Elements", \%theme_taglist]; my $menu = ["Menu Elements", \%theme_menu]; my $other = ["Other Elements", \%theme_other]; return unless ($item); return [$base] if ($item =~ /^\s*(?:b|base)\s*$/i); return [$taglist] if ($item =~ /^\s*(?:l|taglist)\s*$/i); return [$menu] if ($item =~ /^\s*(?:m|menu)\s*$/i); return [$other] if ($item =~ /^\s*(?:o|other)\s*$/i); if ($item =~ /^\s*(?:a|all)\s*$/i) { return [$base, $taglist, $menu, $other]; } say "Invalid Entry: $item\n"; viewTheme(); }

If I call printSettings(viewTheme("all")) it outputs:

============================================================ Base Elements ============================================================ 1. theme.bg_focus => #000000 2. theme.bg_minimize => #ff0000 3. theme.bg_normal => #000000 4. theme.bg_urgent => #ffffff 5. theme.border_focus => #55B043 6. theme.border_marked => #91231c 7. theme.border_normal => #000000 8. theme.border_width => 2 9. theme.fg_focus => #55B043 10. theme.fg_minimize => #ffffff 11. theme.fg_normal => #ffffff 12. theme.fg_urgent => #ffffff ============================================================ Taglist Elements ============================================================ 1. theme.taglist_bg_focus => #000000 2. theme.taglist_bg_normal => #000000 3. theme.taglist_fg_focus => #55B043 4. theme.taglist_fg_normal => #000000 ============================================================ Menu Elements ============================================================ 1. theme.menu_bg_focus => #55B043 2. theme.menu_bg_normal => #000000 3. theme.menu_border_color => #000000 4. theme.menu_border_width => 5 5. theme.menu_fg_focus => #000000 6. theme.menu_fg_normal => #55B043 7. theme.menu_height => 15 8. theme.menu_width => 150 ============================================================ Other Elements ============================================================ 1. theme.font => sans-serif 8: bold 2. theme.wallpaper => /usr/share/awesome/themes/wal +lpapers/ireck.png

Calling an individual group outputs the respective group similarly. Thank you for any input/suggestions

Replies are listed 'Best First'.
Re: Awesome WM config: Lua Source Parse
by space_monk (Chaplain) on Oct 30, 2012 at 14:19 UTC
    Some alternatives - consider a map statement for the inner loop and consider a heredoc for the title. You could probably make the header separator a static too. Something like (not tested):
    sub printSettings { my @groups = shift; my $sep = "=" x 60; foreach my $table (@groups) { foreach my $info (@$table) { my $cnt = 1; print <<EOF; $sep $info->[0] $sep EOF print map { sprintf "%5s. %-25s %-5s $info->[1]\n", $cnt +++, $_;} sort keys {$info->[1]}; } } }
      Thank you space_monk. I had thought of using map, but decided against it. In reading this reply to a previous post of mine Re: Efficiency of map vs. more verbose basic/fundamental code it is suggested that map is better used to create a new list based on modified elements of a previous one, rather than in the printing of a list. I will definitely look into a heredoc for the title, though. I like the way you did that. Thank you again for your suggestions.