Feb 22, 2014, 3:46 AM
Post #12 of 12
The version offered by Kenosis is definitely better practice, I actually considered making the change he suggested but did not do it because I did not want to change too much to the logic of your original script (to let you concentrate on the changes I made) and also because , in that specific simple case, using a global array was in my mind a rather minor defect. So yes, passing a reference to the array into the subroutine is usually better than using directly the global array variable.
Re: [PapaGeek] How to work with "structs" in Perl ??
[In reply to]
... there seems to be a lot of variations and many of the leads I find in the various forums are sort of dead ends
Perl's main motto is: "There is more than one way to do it", often abbreviated as TIMTOWTDI.
Asides from the error in the foreach loop (which I corrected in my first post), your program was correct, clean and perfectly acceptable, even for production code. I offered a refactored version just to to show you some more idiomatic, more "Perlish", ways of doing things. More idiomatic, but probably not more correct, you would be perfectly fine to use your code (with the correction in the loop) as it was. My version is just IMHO shorter, quicker and easier to write, and thus less prone to errors, this is why I wanted to show it to you.
Having said that, if you're really looking for "right" ways of doing things, I would suggest that you be a bit careful with videos and other tutorials on the Internet, some are good but it is not always the case. I think that buying a good book written by recognized authors is probably a good idea. My suggestions would be for the books published by O'Reilly, especially "Learning Perl" to start with and "Programming Perl" to follow up. They are really authoritative.
A lot of what I’ve learned has been trial and error, and that is not the best method for learning things the right way.
I tend to disagree with this. I think everyone of us learn a lot from our errors (and also sometimes from others' errors when we happen to see them). I think that your approach consisting in posting your code on this forum and asking for advice or corrections is very good, you are likely to learn a lot in this way. But, of course, use your judgement to evaluate the comments to receive, not every comment that you receive will be correct.
I hope this helps.