Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
Understanding Perl's Special Variables
Get familiar with Perl's special built-ins

| Input... |

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:

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
--- 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:

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
--- 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:

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
--- 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!
').'
'


How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!


previous page more like this  print this article  next page
 
Search...
 
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback