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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |