Home arrow Perl Programming arrow Page 3 - Understanding Perl's Special Variables

Input... - Perl

Perl comes with a whole bunch of cryptically-named built-invariables, which clever Perl programmers exploit to reduce the number oflines of code in their scripts. This article examines some of the morecommonly-used special variable in Perl, with examples and illustrations ofhow they may be used.

TABLE OF CONTENTS:
  1. Understanding Perl's Special Variables
  2. In Default
  3. Input...
  4. ...And Output
  5. Getting Into An Argument
  6. The Right Path
  7. To Err Is Human
  8. A Question Of Ownership
  9. Rank And File
  10. Calling For A Translator
  11. End Zone
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 38
July 10, 2003

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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!



 
 
>>> More Perl Programming Articles          >>> More By icarus, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: