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

...And Output - 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.

  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



The reverse of the input record separator is the output record separator,
quite logically found in the $\ variable. While the $/ variable deals with
the delimiter used by Perl to break input into discrete records, the $\
variable controls which delimiter Perl uses to separate multiple print()

By default, the output record separator is null, which means that the
output from every call to print() gets attached to the output from the
previous call. Consider the following example, which demonstrates:


# print output
print "The";
print "cow";
print "jumped";
print "over";
print "the";
print "moon";

Here's the output:


You can alter this by specifying a different value from the $\ variable -
as the following example does:


# set output record separator
$\ = " -?- ";

# print output
print "The";
print "cow";
print "jumped";
print "over";
print "the";
print "moon";

Here's the result:

The -?- cow -?- jumped -?- over -?- the -?- moon -?-

Similar, though not identical, is the output field separator, which is used
to specify the delimiter between the different values specified in a single
print() command. This value is stored in the $, variable, and is usually
null. Consider the following example, which demonstrates how it can be used:


# set output field separator
$, = ":";

# print output
print "The", "cow", "jumped", "over", "the", "moon";

Here's the output:


A common use of these two variables is to customize the output of the
print() command - as in the following example:


# load module
use DBI();

# connect
my $dbh = DBI->connect("DBI:mysql:database=test;host=localhost", "root",
"secret", {'RaiseError' => 1}) or die ("Cannot connect to database");

# query
my $sth = $dbh->prepare("SELECT name, age, sex FROM users");

# set separators
$, = ":";
$\ = "\r\n";

# print data as colon-separated fields
# each record separated by carriage return
while(my $ref = $sth->fetchrow_hashref())
print $ref->{'name'}, $ref->{'age'}, $ref->{'sex'};

# clean up

Here's the output:


Sure, this is a very complicated way of doing something really simple - but
hey, it's an example. Don't take it too seriously!

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: