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

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

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

    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.

      Oh but that's not at all the same. It's quite obvious (to me anyway) that $count is being compiled each time. The only reason I brought this up was I didn't know whether the invariant regex in a block attacked to a grep was going to be recompiled each time or not. There's an opportunity for perl to notice that the regex only needs to compiled once - the question here is on whether it takes it or not.

      Update: Oops! I thought you said $line =~ $count where you actually said $line =~ $regex. Anyhow I still think you've misunderstood which question I was asking.


      Fun Fun Fun in the Fluffy Chair