Perl Programming Page 10 - Perl 101 (Part 4) - Mind Games |
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.
blog comments powered by Disqus |