$/ is called the "input record separator". Every text line that you would read from a text file will contain "\n" at the end and Perl would say "hey that's one 'record'" because $/ is '\n' by default.

my $line_count=1; while (<FILEHANDLE>) { print $line_count++, " :$_"; }
Above prints a "line number" for each line. And yes there is a special Perl variable that represents that without having to use $line_count. But I figure that Perl is terse enough without needing it here. Above, each thing in $_ will be delimited by the input record separator. I used $line_count to show that there are indeed separate input lines being processed.

If you "undefine" the input record separator from its default of "\n", then the whole file will be read in one single shot! That's because there is no "stopping place" to define what a input record (in this case a "line").

local is a weird critter...it says that in this scope define var $x to be "whatever". In functional terms this is like (when re-defined within a scope): push current value, use this new value, pop previous value when the scope changes.

In previous code,

$/ = undef; my $lines = <STATFILE>;
would have the same effect as:
my $lines = do { local $/; <STATFILE> };

$/'s meaning is not FILEHANDLE specific and if you had multiple files open for read at once, the current value of $/ would apply to any subsequent read from any file, but you only had one file.

This "do {code}" is like a subroutine. You will probably write a lot of code before you really need this. Anyway, after this statement, $/ is auto-magically set back to the default of "\n".

Reading a whole file at a time into memory is good for some situations and is good for records where fields span multiple lines. But you don't have that. You have a record that although it contains multiple input lines, nothing spans a line (a "\n" boundary). This is typical of well designed log file output. Log files often get very large and the ability to process them while only requiring a small subset of lines at a time in memory is often well worth the trouble!

I demonstrated one classic way to process a multi-line record, the technique works in C, JAVA or any language. Perl has some cool other ways. For more reading, check out Flipin good, or a total flop?. Perl has a "range" operator that decides whether you are inside of 2 lines bounded by a regular expression. This allows processing in a similar way to the "classic" method and does not require that the entire log file be read into memory at once...again..often a huge advantage!

Hope this lengthy reply helped. There is a BIG difference between records whose fields span multiple lines and a record that contains a collection of more than one line. Well designed text log file formats do not have the former.


In reply to Re^3: multiple regexp matches in multi line string by Marshall
in thread multiple regexp matches in multi line string by wirelesscharlie

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.