Home arrow Perl Programming arrow Page 10 - Perl 101 (Part 4) - Mind Games

Miscellaneous Stuff - Perl

This week's article teaches you how to use Perl to interact withfiles on your system, and also provides you with a quick crash course invarious array functions.

TABLE OF CONTENTS:
  1. Perl 101 (Part 4) - Mind Games
  2. Handle With Care
  3. Different Strokes
  4. A Little Brainwashing
  5. Die! Die! Die!
  6. Testing Times
  7. Popguns And Pushpins
  8. Shifting Things Around
  9. The Real World
  10. Miscellaneous Stuff
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 11
June 29, 2000

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
The @ARGV variable
------------------
Perl allows you to specify additional parameters to your program on the command line - these parameters are stored in a special array variable called @ARGV. Thus, the first parameter passed to a program on the command line would be stored in the variable $ARGV[0], the second in $ARGV[1], and so on.

Using this information, you could re-write the very first file-test example above to use @ARGV instead of the <STDIN> variable for input:
#!/usr/bin/perl
if (-e $ARGV[0])
{
print "File exists!\n";
}
else
{
print "File does not exist!\n";
}

<STDIN> and other file handles
------------------------------
Perl comes with a few pre-defined file handles - one of them is <STDIN>, which refers to the standard input device. Additionally, there's <STDOUT>, which refers to the default output device [usually the terminal] and <STDERR>, which is the place where all error messages go [also usually the terminal].

Logical Operators
-----------------

You've probably seen the || and && constructs in the examples we've shown you over the past few lessons. We haven't explained them yet - and so we're going to rectify that right now.

Both || and && are logical operators, commonly used in conditional expressions. The || operator indicates an OR condition, while the && operator indicates an AND condition. For example, consider this:
if (a == 0 || a == 1)
{
do this!
}

In English, this would translate to "if a equals zero OR a equals one, do this!" But in the case of
if (a == 0 && b == 0)
{
do this!
}

the "do this!" statement would be executed only if a equals zero AND b equals zero.

Perl also comes with a third logical operator, the NOT operator - it's usually indicated by a prefixed exclamation mark[!]. Consider the two examples below:
if (a)
{
do this!
}

In English, "if a exists, do this!"
if (!a)
{
do this!
}

In English, "if a does NOT exist, do this!"

And that's about it for this week. We'll be back in a couple of weeks with more Perl 101. Till then...stay healthy!

This article copyright Melonfire 2000. All rights reserved.

 
 
>>> More Perl Programming Articles          >>> More By Vikram Vaswani and Harish Kamath, (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: