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

Slightly dangerous if the application calls for using the regex in a loop where one of the variables (eg. $month) changes, as unless the qr// is also inside the loop (in which case the optimisation benefits are lost), as the changing value of the variable won't be recognised.


Examine what is said, not who speaks.

Replies are listed 'Best First'.
Re^5: Grep Pattern Match
by diotalevi (Canon) on Dec 27, 2002 at 16:21 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

Re: Re: Re^3: Grep Pattern Match
by LostS (Friar) on Dec 27, 2002 at 16:23 UTC
    I am not really going for optimisation (yet)... just want a script that will parse my log files that will search for attempted DOS's and stuff and then take those IP's and add them to our deny list. That is all I am writing this for.

    -----------------------
    Billy S.
    Slinar Hardtail - Hand of Dane
    Datal Ephialtes - Guildless
    RallosZek.Net Admin/WebMaster

    perl -e '$cat = "cat"; if ($cat =~ /\143\x61\x74/) { print "Its a cat! +\n"; } else { print "Thats a dog\n"; } print "\n";'