P is for Practical | |
PerlMonks |
Scope and Contextby rob_au (Abbot) |
on Nov 14, 2001 at 05:17 UTC ( [id://125195]=perlmeditation: print w/replies, xml ) | Need Help?? |
While working on a project for a client recently, I wrote an interesting piece of code, that after I wrote raised more questions than it solved. The summarised code, edited for clarity and focus, is as follows:
The intent of this code is to load a series of regular expressions from a file, precompile them and store them in a match array which is referenced later in the program. Once written however, it raised a number of questions in my mind as to the 'correctness' of this code. Consider the following:
Traditionally, the use of map in this context, without immediate assignment of the results, would be a bad thing. The "preferred" programming style something more like this:
Yet in the context of an eval statement where the results are being caught and assigned outside the scope of the block, such lapses in programming style can (I hope) be excepted. Is this 'correct' style though? I am not disregarding the returned results of the map statement from a block level, but from a statement level I am committing a heinous crime Furthermore, the code within the eval itself ...
... One will note that there is no close statement for the opened file handle. Rather than implicitly closing my filehandle, I have relied upon the local scope of the FH file handle. While certainly not "incorrect", this aspect of the code still made me stop and pause upon review. There is also the use of the underscore filehandle in the -r file test, but this syntax has been discussed extensively previously. Am I approaching Perl with a style too pragmatic, taking the rope that Perl gives me and slowly tying a noose? Or is this approach welcomed as a more practical approach and exploitation of the feature-set provided?
Ooohhh, Rob no beer function well without!
Back to
Meditations
|
|