Understanding Perl's Special Variables - Input... (Page 3 of 11 )
Another very useful variable is the $/ variable, which Perl calls the input
record separator. The $/ variable contains one or more characters that
serve as line delimiters for Perl; Perl uses this variable to identify
where to break lines in a file.
In order to illustrate, consider the following sample password file:
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
news:x:9:13:news:/etc/news:
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
By default, the $/ variable is set to the newline character. Therefore,
when Perl reads a file like the one above, it will use the newline
character to decide where each line ends - as illustrated in the following
example:
#!/usr/bin/perl
# read file into array
open (FILE, "dummy.txt");
@lines = <FILE>;
# iterate over file and print each line
foreach $line (@lines)
{
print "--- " . $line;
}
# print number of lines in file
$count = @lines;
print "$count lines in file!\n";
Here's the output:
--- shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
--- halt:x:7:0:halt:/sbin:/sbin/halt
--- mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
--- news:x:9:13:news:/etc/news:
--- uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
--- operator:x:11:0:operator:/root:/sbin/nologin
--- games:x:12:100:games:/usr/games:/sbin/nologin
--- gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
--- ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
--- nobody:x:99:99:Nobody:/:/sbin/nologin
10 lines in file!
Now, if you alter the input record separator, Perl will use a different
delimiter to identify where each line begins. Consider the following
example, which sets a colon (:) to be used as the delimiter, and its
output, which illustrates the difference:
#!/usr/bin/perl
# set input record separator
$/ = ":";
# read file into array
open (FILE, "dummy.txt");
@lines = <FILE>;
# iterate over file and print each line
foreach $line (@lines)
{
print "--- " . $line;
}
# print number of lines in file
$count = @lines;
print "$count lines in file!\n";
Here's the new output:
--- shutdown:--- x:--- 6:--- 0:--- shutdown:--- /sbin:--- /sbin/shutdown
halt:--- x:--- 7:--- 0:--- halt:--- /sbin:--- /sbin/halt
mail:--- x:--- 8:--- 12:--- mail:--- /var/spool/mail:--- /sbin/nologin
news:--- x:--- 9:--- 13:--- news:--- /etc/news:---
uucp:--- x:--- 10:--- 14:--- uucp:--- /var/spool/uucp:--- /sbin/nologin
operator:--- x:--- 11:--- 0:--- operator:--- /root:--- /sbin/nologin
games:--- x:--- 12:--- 100:--- games:--- /usr/games:--- /sbin/nologin
gopher:--- x:--- 13:--- 30:--- gopher:--- /var/gopher:--- /sbin/nologin
ftp:--- x:--- 14:--- 50:--- FTP User:--- /var/ftp:--- /sbin/nologin
nobody:--- x:--- 99:--- 99:--- Nobody:--- /:--- /sbin/nologin
61 lines in file!
You can even undef() the record separator, which will result in Perl
reading the entire file into a single string. Take a look:
#!/usr/bin/perl
# remove input record separator
undef($/);
# read file into array
open (FILE, "dummy.txt");
@lines = <FILE>;
# iterate over file and print each line
foreach $line (@lines)
{
print "--- " . $line;
}
# print number of lines in file
$count = @lines;
print "$count lines in file!\n";
And here's the new output, which clearly displays that the file has been
read as a single line:
--- shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
news:x:9:13:news:/etc/news:
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
1 lines in file!
Next: ...And Output >>
More Perl Articles
More By icarus, (c) Melonfire