in reply to Scope and Context

I hope you won't take this the wrong way, but that code is very hard to read. It's cool to do things like this with eval, but not a good idea from a maintenance perspective most of the time. I would have arranged it something like this (untested):
my @matches = get_matches($file); sub get_matches { my $file = shift; return unless (-e $file && -r _); local *FH; sysopen FH, $file, O_RDONLY; flock FH, 8; map { chomp; qr/$_/ } <FH>; }
I don't think the map is a big deal here, since you want the return value. I'm not sure it's worth messing around with sysopen though (I've never needed to use it, so I don't even know if it still works the way I rearranged it), and hard-coding the constant "8" is probably not a good idea either. I expect you know all this and were just posting to point out other things, but maybe someone else reading this didn't.

Replies are listed 'Best First'.
Re: Re: Scope and Context
by rob_au (Abbot) on Nov 14, 2001 at 07:02 UTC
    I hope you won't take this the wrong way, but ...

    Nah, never taken the wrong way ... *memo to self - add perrin to the list* ... :)

    I understand your point with regard to readability ... I have been guilty of writing some horrendously difficult code to decipher before, purely without intent - The reason for the embedding of the subroutine within the code at this point is so that the regular expressions in the file are loaded entirely within the BEGIN {} block. Yes I can still call the subroutine from this point, but for my own mindset, I like keeping such initialisation code in the one place. Nevertheless, your point is well noted ...

    The hard-coding of the constant 8 is more of an oversight, but one which I should most definitely correct - Guess that's what happens when you get too pensive regarding programming methodology and ignore the practicalities *grin*

     

    Ooohhh, Rob no beer function well without!

      The reason for the embedding of the subroutine within the code at this point is so that the regular expressions in the file are loaded entirely within the BEGIN {} block. Yes I can still call the subroutine from this point, but for my own mindset, I like keeping such initialisation code in the one place.

      You could put the sub definition in the BEGIN block...

        If you're never going to call the sub from outside the BEGIN block then maybe something like:
        BEGIN { my $get_regexes = sub { ... return map {...} }; ... }
        Which has the advantage of keeping the subroutine private.

        Oh yes, don't forget that in recent perl you can do open my $fh, ..., which is far nicer than

        local *FH; open FH, ...