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

On p.55 of J.Friedl's book "Mastering Regular Expressions" he does the following:
$/ = ".\n";
And says that this puts the input in "paragraph-mode".

But I dont understand this... how could this put the input in paragraph mode? It is simply saying split input anywhere that a period is followed by a newline, which could conceivably be somewhere inthe middle of paragraph, eg.

Gotcha Jeffery. See you thought you could fool me

Replies are listed 'Best First'.
Re: Setting Input record separator to chunk on paragraphs
by peters (Sexton) on Aug 20, 2000 at 03:23 UTC
    Right, $/ isn't interpreted as a regex, just as a fixed token to split on. It has a few special cases, one of which is "" - this is what it calls "paragraph mode", not to be confused with jfriedl's pattern, which splits wherever a line ends in a period. $>="" splits on two or more consecutive blank lines - /\n\n+/ if you could do it that way. And setting $/ to undef causes it to read to end of file. You can also set it to a reference to an integer for fixed length record reading... see perldoc perlvar.
(jeffa) Re: Setting Input record separator to chunk on paragraphs
by jeffa (Bishop) on Aug 20, 2000 at 02:40 UTC
    He is assuming that all paragraphs end that way. For example, these two paragraphs would be counted as one:

        One day I found myself at the river, again thinking
    about what happened that day. The water was cold,
    didn't really help me relax. What a predicament!
        How could someone be so cold? Did I say
    the wrong thing? And where was Charlie Sheen? I mean,
    this was all his fault.

    As you can see, there are clearly two paragraphs, but the last sentance of the first one breaks our "paragraph mode." So yes, you are correct, this doesn't catch every paragraph. We would need to specify that question marks and exclamation points would also constitute the end of a paragraph.

    I tried to set $/ to a character class, but I don't think it is possible. The Camel Book says that it can be set to a multi-character delimiter, but nothing about a class of characters.

    I assume that the author was using 'auto-wrap' and never actually typing a newline until the end of a paragraph.

    hope this helps,
    jeff

      It says at http://etext.lib.virginia.edu/helpsheets/regex.html that ". Matches any one character " So therefore "." would mean to split anywhere that a character is followed by a line return.
RE: Setting Input record separator to chunk on paragraphs
by merlyn (Sage) on Aug 21, 2000 at 00:45 UTC