smackdab has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I am reading some text files and applying a regex to every line and keeping the ones that match.

This works as expected...but I also want to make it generic. (I save the regex string out to a file)

By walking line-by-line, do I mess up the "=~ m//sm" options? Do I need a flag that says to apply the regex on a line-by-line basis or just to the whole string?

Hope this makes sense ;-)

thanks

Replies are listed 'Best First'.
Re: generic regex parsing
by Zaxo (Archbishop) on Sep 08, 2002 at 07:25 UTC

    Those options will do no harm if you read line by line, they will simply have no effect.

    After Compline,
    Zaxo

Re: generic regex parsing
by rir (Vicar) on Sep 08, 2002 at 20:01 UTC
    I am not sure I understand your question.

    What do you mean by "the regex string"?

    The options will work on whatever you pass to them.

    The regex pattern is applied to whatever is the bound value,
    the left hand side of the =~ op.

    Your modifiers, the trailing "sm" in "//sm" don't care
    where the bound value comes from.
    The 's' means that '.' matches '\n', the 'm' means
    that '^' and '$' see the '\n's in your string and match
    beside them. Then only \A and \Z anchor at the beginning
    and end of your string.

    When you speak of genericity, I think you might be
    considering using $some_variable for your PATTERN.
    Beware of having a null string as your PATTERN.
    When you do you get the previous PATTERN which wasn't
    hidden in an narrower scope. This is not apt to be what
    you want.

    I may just be seeing myself
    (psych or metafizz or both? I don't know)
    but I think you are doing well. The binding and
    the matching of regular expressions can be an unusual
    concept to wrap your mind around.
    It is a large swiss army chain saw in itself, learn it
    bit by bit.

    Go at it with a calm mind.