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

Getting Into An Argument - 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

Perl comes with some very interesting variables specifically designed to
store input arguments, for both scripts and subroutines. The first of these
is the special @ARGV array, which contains a list of all the command-line
arguments passed to the Perl program; each argument is indexed as an
element of the array. Consider the following example, which demonstrates:


#!/usr/bin/perl

# get length of argument list
$num = @ARGV;

# iterate and print arguments
for ($x=0; $x<$num; $x++)
{
print "Argument " . ($x+1) . " is $ARGV[$x]\n";
}

Here's an example of the output (I called this script with the command-line
arguments "red 5px Arial"):


Argument 1 is red
Argument 2 is 5px
Argument 3 is Arial

Perl also comes with a variable named @_, which contains arguments passed
to a subroutine, and which is available to the subroutine when it is
invoked. The value of each element of the array can be accessed using
standard scalar notation - $_[0] for the first element, $_[1] for the
second element, and so on.

In order to illustrate, consider the following example:


#!/usr/bin/perl

# define a subroutine
sub add_two_numbers
{
$sum = $_[0] + $_[1];
return $sum;
}

$total = &add_two_numbers(3,5);
print "The sum of the numbers is $total\n";

In the example above, once the &add_two_numbers subroutine is invoked with
the numbers 3 and 5, the numbers are transferred to the @_ variable, and
are then accessed using standard scalar notation within the subroutine.
Once the addition has been performed, the result is returned to the main
program, and displayed on the screen via the print() statement.



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