Perl 101 (Part 4) - Mind Games - Miscellaneous Stuff
(Page 10 of 10 )
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.| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |