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

A Question Of Ownership - 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



You can obtain information on the user and group the Perl script is running
as, with the following four variables:

$< - the real UID of the process

$> - the effective UID of the process

$) - the real GID of the process

$( - the effective GID of the process

A difference between "real" and "effective" IDs appears when you use the
setuid() or setgid() command to change the user or group. The "real" ID is
always the one prior to the setuid() or setgid() call; the "effective" one
is the one you've changed into following the call.

Consider the following example, which demonstrates:


# print real UID
print "Real UID: $<\n";

# print real GID
print "Real GID: $(\n";

# print effective UID
print "Effective UID: $>\n";

# print effective GID
print "Effective GID: $)\n";

Here's the output:

Real UID: 515
Real GID: 100 514 501 100
Effective UID: 515
Effective GID: 100 514 501 100

Notice that the $) and $( commands return a list of all the groups the user
belongs to, not just the primary group.

Of course, most often this is not very useful by itself. What you really
need is a way to map the numeric IDs into actual user and group names. And
Perl comes with built-in functions to do this - consider the following
example, which illustrates:


# set record separator
$\=" ";

# print user and group
print "This script is running as " . getpwuid($>) . " who belongs to the
following groups:";

foreach (split(" ", $))) { print scalar(getgrgid($_)); };

Here's the output:

This script is running as john who belongs to the following groups: users
software apps

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