|P is for Practical|
Re^3: very new to perl; suggestions for porting this shell script to perl?by Cristoforo (Deacon)
|on May 29, 2011 at 20:35 UTC||Need Help??|
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.
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.