This program explains how the $/ record separator charactor works. Setting it equal to different values changes the behavior of the angle brackets operators.

I got interested in this when I tried to explain to someone how to read more than one line at a time from a data file to use a regex to get rid of duplicate lines.
BTW, as jeroenes says, there is an explanation of this at:
How can I extract just the unique elements of an array?.

Note the interesting way we have to "rewind" the DATA file handle. I got this from perlmonks:
re-opening DATA, printing any character

Understand that the $/ is not a regex. It can't contain, for example, character classes or alternatives. While it can consist of more than one character, trying to consistantly use this to read, for example, paragraphs, won't work, because a paragraph can end in different combinations of characters.
See Setting Input record separator to chunk on paragraphs.

=head1 NAME record_separator.pl - demonstrates how the $/ record separator works =head1 AUTHOR zeno =head1 SYNOPSIS perl record_separator.pl =head1 DESCRIPTION This program explains how the $/ record separator charactor works. Setting it equal to different values changes the behavior of the angle brackets operators. I got interested in this when I tried to explain to someone how to read more than one line at a time from a data file to use a regex to get rid of duplicate lines. See also: How can I extract just the unique elements of an array? http://www.perlmonks.com/index.pl?node_id=609 Note the interesting way we have to "rewind" the DATA file handle. I got this from perlmonks: "re-opening DATA, printing any character" http://www.perlmonks.com/index.pl?node_id=23403 Understand that the $/ is not a regex. It can't contain, for example, character classes or alternatives. While it can consist of more than one character, trying to consistantly use this to read, for example, paragraphs, won't work, because a paragraph can end in different combinations of characters. See "Setting Input record separator to chunk on paragraphs": http://www.perlmonks.com/index.pl?node_id=28681 =cut use strict; use warnings; # record the start of the DATA (if we don't do this and try # to seek(DATA,0,0), we start reading again from the start # of the program! interesting, but not very useful. my $startOfData= tell DATA; print "HOW DOES THE END OF RECORD SEPARATOR WORK?\n"; print "Normal behavior (\$/ = newline)\n"; while (<DATA>) { chomp; print "I read ->$_<-\n"; } seek( DATA, $startOfData, 0 ) or die "Can't seek: $!"; print "\nRead word for word (\$/ = ' ')\n"; print "Careful here! there isn't always a space "; print "between words! \n"; print "And sometimes there's more than one!\n"; $/ = " "; while (<DATA>) { chomp; print "I read ->$_<-\n"; } seek( DATA, $startOfData, 0 ) or die "Can't seek: $!"; print "\nRead sentences (\$/ = '.')\n"; $/ = "."; while (<DATA>) { chomp; print "I read ->$_<-\n"; } seek( DATA, $startOfData, 0 ) or die "Can't seek: $!"; print "\nRead psuedo-paragraphs (\$/ = '.\\n')\n"; print "Careful here! Not all paragraphs end this way!\n"; $/ = ".\n"; while (<DATA>) { chomp; print "I read ->$_<-\n"; } seek( DATA, $startOfData, 0 ) or die "Can't seek: $!"; print "\nsuck mode (undef \$/)\n"; undef $/; while (<DATA>) { chomp; print "I read ->$_<-\n"; } seek( DATA, $startOfData, 0 ) or die "Can't seek: $!"; print "\nParagraph mode (\$/ = '')\n"; $/ = ''; while (<DATA>) { chomp; print "I read ->$_<-\n"; } seek( DATA, $startOfData, 0 ) or die "Can't seek: $!"; print "\nRead a certain number of chars (\n\$/ = \\12)\n"; $/ = \12; while (<DATA>) { chomp; print "I read ->$_<-\n"; } __DATA__ Hello there mom. I'm sorry I haven't written for a while. All is well! Everything is fine. You're great.

In reply to Record Separator Study Aid by zeno

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.