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:

#!/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!


In reply to Scope and Context by rob_au

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.