Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: very new to perl; suggestions for porting this shell script to perl?

by Cristoforo (Curate)
on May 28, 2011 at 16:25 UTC ( #907140=note: print w/replies, xml ) Need Help??

in reply to very new to perl; suggestions for porting this shell script to perl?

Not knowing Unix systems, I can't offer too much help. It would be very helpful to provide about 50+ lines of your output to show how you want the results of the program. I'm sure there are many improvements that could be made. For one, the line:


It could be better stated:

my @Files = grep /\.desktop$/, readdir DIR;

grep will only pass *.desktop files to @Files

If you plan on writing Perl scripts, it would be advisable to declare your variables, (usually with 'my'), and to include the lines (at the beginning of your script):

#!/usr/bin/perl use strict; use warnings;

These will tell you of errors in your code syntax, spelling mistakes, etc. You will find nearly all the code on this site follow that method.

Update: It would also be helpful to see some of the input files, in paticular, lines that match Categories and Name and Exec.

One last thing - it would be nice to enclose these sample inputs and outputs in <readmore>. . .</readmore> tags so your post doesn't take so much space. :-)

Replies are listed 'Best First'.
Re^2: very new to perl; suggestions for porting this shell script to perl?
by sinusoid (Initiate) on May 29, 2011 at 00:51 UTC

    That first suggestion is very helpful. 2 completely irrelevant files were being searched.

    If you're more experienced with windows, those desktop files are basically like a more detailed version of a windows shortcut file. They're installed to /usr/share/applications upon installing most linux gui apps.

    Here are the contents of gimp.desktop.


    Here is what's generated by the script from my .desktop files:

    With certain applications I had previously installed, the desktop files would occasionally contain one or more sections in addition to the [Desktop Entry] section , which would often contain another Name=xxxx and Exec=xxxx line. This is why I would like to find a way to either limit the regex match to only the first instance or else be able to specify a start/stop range for the matching.

    I appreciate your willingness to help despite not having much to go by. Thanks alot.

      Yes, the sample files and output clarified the problem.

      Here is a solution, but you should probably check it to see if it meets your specifications.

      #!/usr/bin/perl use strict; use warnings; my @files = glob "/usr/share/applications/*.desktop"; my $regex = join "|", qw/ Audio Graphics Network Settings System Utili +ty /; my %data; FILE: for my $file (@files) { open my $fh, "<", $file or die "Unable to open $file (for reading) +. $!"; my ($category, $name, $exec); while (<$fh>) { if(/^Categories=.*?\b($regex)\b/) { $category ||= $1; # sets cat for first time only (each fil +e) } elsif (/^Name=(.+)$/) { $name ||= $1; # sets name first time only (each file) } elsif (/^Exec=(\S+)/) { $exec ||= $1; # sets exec first time only (each file) } } close $fh or die "Unable to close $file. $!"; for ($category, $name, $exec) { if (! defined) { warn "$file missing category or name or exec.\n"; # skip the 'push' below and go to the label 'FILE:' next FILE; } } push @{ $data{ $category } }, {name => $name, exec => $exec}; } for my $category (keys %data) { print "Submenu = \"$category\" {\n"; my @recs = @{ $data{ $category } }; for my $href ( @recs ) { # hash reference print " Entry = \"$href->{name}\" { Actions = \"$href->{ex +ec}\" }\n"; } print "}\n"; }

      Notice how all the variables are declared with my. Declaring variables this way helps catch spelling errors and also limits the scope to the innermost containing block. This property is handy and can be seen in the declaration of my ($category, $name, $exec)

      Every time the for loop starts a new iteration, (with a new file), it gets a fresh copy of 'undefined' variables. Then, later on they are assigned to, ($category ||= $1;). The first time a valid match is made, an assignment will happen. If there are more than 1 instance of name or exec (as you noted in your post), they will be ignored. This is because any of the three already has the first match assigned to it.

      The data is stored in the %data hash (which is then printed out when the loop terminates).

      The glob function does wildcard expansion and returns all the matching files. (gimp.desktop and all other files with the .desktop extension).

      There are many other Perl idioms here and if you have questions about just why a certain piece is written the way it is, just ask.

      If you plan on using Perl now on, it would be probably helpful to read a book such as Learning Perl witch is a good intro to Perl or Programming Perl which is larger but a good reference book.

        It's working great! I've only made a few changes. I changed the (/^Exec=(\S+)/) back to (/^Exec=(.*)/) because I still want to preserve the flags present at the end of some "Exec=xxx" lines (for example: "Exec=pcmanfm-mod --find-files" which corresponds to a "Name=Search for Files and Folders"), while still excluding %f, %F, %u and %U from any entries, because these act as placeholders for the filenames or urls on which the user has right-clicked.

        The new script did create a small issue, however. I noticed that it prints the submenus in a different order from how they're listed. Do you know of any simple ways to make it respect the order in which they're specified when they're assigned at "my $regex ="? If not, it's completely usable how it is and I'll keep it in mind as I'm reading some of the books you suggested.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://907140]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2018-02-18 20:32 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (257 votes). Check out past polls.