I have very limited experience in any sort of scripting other than linux shell scripts. I wrote a shell script to generate a menu for a linux window manager called pekwm, but it wasn't quite fast enough to be used as a "dynamic menu entry" so i've been using it to generate a text file read by the menu on a per-need basis. Then i came across perl and noticed how much more speed the scripts seem to be capable of achieving and decided to try to port the shell script to perl. I do not want to add any functionality, i just have a feeling it could be much more efficient, as i wrote based on nothing but a few relevant examples I came across. For a more detailed description on how this script is supposed to work see the very bottom. First i'll post the shell script (which i don't believe needs any refinement, followed by what i've managed in perl..

Shell Script:
CATS="Audio Graphics Network Settings System Utility" for CAT in $CATS; do echo " Submenu = \"$CAT\" {" for CATMATCH in `grep -l "^Categories=.*$CAT.*" /usr/share/applica +tions/*.desktop`; do name=`sed -n '1,/^Name=/ s/^Name=//p' <$CATMATCH` exec=`sed -e 's/ %[UuFf]//' -ne '1,/^Exec=/ s/^Exec=//p' < +$CATMATCH` echo " Entry = \"$name\" { Actions = \"Exec $exec\" + }" done echo " }" done ##SEDLESS EQUIVALENT## #CATS="Audio Graphics Network Settings System Utility" #for CAT in $CATS; do # echo " Submenu = \"$CAT\" {" # for CATMATCH in `grep -l "^Categories=.*$CAT.*" /usr/share/applic +ations/*.desktop`; do # name=`grep "^Name=" $CATMATCH` # exec=`grep "^Exec=" $CATMATCH`; exec=${exec% %[FfUu]} # echo " Entry = \"${name#Name=}\" { Actions = \"Exe +c ${exec#Exec=}\" }" # done # echo " }" #done


One thing I was unable to figure out how to emulate in perl was the option to specify a set of expressions or line numbers fuctioning as the start/stop boundaries for where the string search/replace should happen. this was achieved in the shell script above using "name=`sed -n '1,/^Name=/ s/^Name=//p' <$CATMATCH`", where "1,/^Name=/" specifies the boundaries.

Perl Script:
@Cats = ("Audio","Graphics","Network","Settings","System","Utility"); $Dir = "/usr/share/applications"; opendir(DIR,"$Dir"); @Files=readdir(DIR); foreach $Cat (@Cats) { print "Submenu = \"$Cat\" { \n"; foreach $File (@Files) { open FILE, "$Dir/$File"; while (<FILE>) { if(/Categories=.*$Cat.*/) { @Matches = $File; foreach $Match (@Matches) { open MATCH, "$Dir/$Match"; while (<MATCH>) { #$string = $_; @Names = $_=~ (/^Name=(.*)/o); @Execs = $_=~ (/^Exec=(.*)/o); foreach $Name (@Names) { print " Entry = \"$Name\" "; }; foreach $Exec (@Execs) { $Exec =~ s/(.*) %[uUfF]/$1/; print "{ Actions = \"Exec $Exec\" }\n"; }; }; }; }; }; }; print "}\n"; };


The script generates a menu (actually, a part of a menu) by matching the desired list of categories (which are also used as names for each sub-menu entry during generation) against those specified in the /usr/share/applications/*.desktop files. When a match is found, it reports the name of which xxx.desktop file the match occured and extracts from that file the name of the application and the command used to run it. It then prints the necessary menu syntax stuff, adding from variables the name of the sub-menu (taken from the matched category), the name of the application (taken from the name extracted from the xxx.desktop file), and the command used to execute that application (also extracted from the xxx.desktop file) for each particular file of each particular submenu. Thanks in advance for any sort of help you can give!

In reply to very new to perl; suggestions for porting this shell script to perl? by sinusoid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.