#!/usr/bin/perl -Tw use strict; use warnings; my $file = "/usr/local/app/etc/app.conf"; my @match = eval { use Fcntl qw/:DEFAULT/; local *FH; sysopen FH, $file, O_RDONLY; flock FH, 8; map { chomp; qr/$_/ } <FH>; } if (-e $file && -r _);
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:
map { chomp; qr/$_/ } <FH>;
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:
my @match = eval { use Fcntl qw/:DEFAULT/; local *FH; sysopen FH, $file, O_RDONLY; flock FH, 8; my @map = map { chomp; qr/$_/ } <FH>; return @map; } if (-e $file && -r _);
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 ...
{ use Fcntl qw/:DEFAULT/; local *FH; sysopen FH, $file, O_RDONLY; flock FH, 8; map { chomp; qr/$_/ } <FH>; }
... 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!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Scope and Context
by dws (Chancellor) on Nov 14, 2001 at 05:56 UTC | |
Re: Scope and Context
by jynx (Priest) on Nov 14, 2001 at 06:29 UTC | |
Re: Scope and Context
by perrin (Chancellor) on Nov 14, 2001 at 06:55 UTC | |
by rob_au (Abbot) on Nov 14, 2001 at 07:02 UTC | |
by perrin (Chancellor) on Nov 14, 2001 at 07:13 UTC | |
by pdcawley (Hermit) on Nov 14, 2001 at 20:02 UTC | |
(tye)Re: Scope and Context
by tye (Sage) on Nov 14, 2001 at 23:16 UTC | |
Re: Scope and Context
by mugwumpjism (Hermit) on Nov 19, 2001 at 15:02 UTC | |
Re: Scope and Context
by petral (Curate) on Nov 14, 2001 at 20:41 UTC |