in reply to Re: Re^3: Grep Pattern Match
in thread Grep Pattern Match

Are you sure? I thought that the regex was being recompiled on each iteration to that block which is why I posted that.


Fun Fun Fun in the Fluffy Chair

Replies are listed 'Best First'.
Re: Re^5: Grep Pattern Match
by BrowserUk (Patriarch) on Dec 27, 2002 at 16:39 UTC

    Um, no. Not sure, sure, but that is what I believe to be the case following various discussions regarding the use of qr// and the contrast between it, using /o on a specific m// or s///, and the utility (or otherwise) of using the /o operator in conjuction with qr//o.

    What I think (though) that the concensus was that using /o on the qr// operator was a waste of time as qr// effectively already did once-only compilation.

    It's quite possible I have misinterreted this (again), and given that it's you that challenging my assumptions, and your likely to reply with a Devel::* dump demostrating that this is not the case, I'll turn the statement into a question and ask....Is this slightly dangerous?


    Examine what is said, not who speaks.

      I threw some debug-foo at this code fragment and came up with yes and no. Looking at the code via B::Concise it looks like the regex is compiled each time. Looking at the code view use re 'debug' it looks like it's compiled only once. I suppose that enough digging through perl's guts would provide a real answer but really -- I'd rather just use qr to control when the regex is compiled and not have to worry about what the internals are doing.

      I mean, there are times it's interesting and useful to know what's happening but this case is just too opaque to get at.

      my @in = qw(nc01123.log nc02123.log nc03123.log); my $year = '0'; my $month = '2'; @in = grep { /^nc$year$month.*\.log$/ } @in; print "@in\n"; __END__ Try zero: The nested sub block was hard to read so this didn't yield t +oo much. perl -MO=Concise test.pl Try one: This makes it look like the regex is compiled anew each time. perl -MO=Concise,-exec test.pl Try two: This looks like the regex is compiled only once. Or maybe deb +ug just <i>dumps</i> it once. use re 'debug';

      Fun Fun Fun in the Fluffy Chair

        A rather simpler test method, but in this, (only) "line 1" is printed out 10 times.

        #! perl -sw use strict; my $count=1; my $regex = qr/$count/; my $told = tell DATA; my $line; for $count (1 .. 10) { $line =~ $regex and print $line while $line = <DATA>; seek DATA, $told, 0; } __DATA__ line 1 line 2 line 3 line 4 line 5 line 6 line 7 line 8 line 9

        Examine what is said, not who speaks.