08-22-2011, 02:30 PM
Here's a tutorial that will help users learn about ARGV in perl.
ARGV has a few different meanings depending on how it's defined and used, but for this tutorial we will say that it means command line arguments for when the script is told to be interpreted.
For example, what I mean is that these would be ARGV values: <*colored in blue*>
We can now split each like we do in an array to use the value of each as a variable and use if statements in perl to make sure that all of the arguments are filled.
To start, we will use an if statement to define an output if there are no arguments. An example of this would be if someone tried to interpret a perl script normally like this:
I usually print out a little title menu like this to show the person trying to run my perl script that you need to use a certain syntax for it to work, in this case something like:
If there are no arguments:
The title menu will show up because of this if statement here:
And nothing else in the script will be read because we told it to exit after the "menu" is displayed:
Now we can go on to define what to respond when not enough arguments have been given a value. So if we are looking for 3 arguments, and the person uses this command:
Only 2 arguments have been defined for us to use, and that's no good. Therefor we can add onto our if statement with something like this to show that the person has not specified enough arguments like the syntax told them to do:
This tells the script to show/"print out" a response ("You have not provided all of the arguments required") if 3 arguments have not been given a value in the first command input (ARG 1 = 0, ARG 2 = 1, ARG 3 = 2). 0 is a placeholder for the first argument, therefor 0-2 is actually 3 arguments. So in our script, when we say $#ARGV < 2, we actually mean if it's less than 3 arguments. Then we exit with "exit;" like we did in the last example so that none of the rest of the script can be read.
If everything is good, and the user enters the syntax properly, now we can use those values and turn them into variables:
From this command input, think of it this way; blue = first argument ($ARGV[0]), green = second argument ($ARGV[1]), and orange = third argument ($ARGV[2]):
To show those variables that we've given values to our arguments, now we can simply just print them out:
*Note that \n is a special entry for a new line
Full Script:
For a visual on how this script works, i've initiated all outcomes in this image here:
*Blue Box - We have entered something like this in the command console:
*Purple Box - We have entered something like this in the command console: (Missing an argument/parameter in the syntax)
*Orange Box - We have entered something like this in the command console: (All arguments/parameters have been given values and the syntax has been correctly inputted)
I personally put this test script together for learning purposes. I hope you'll enjoy it.
Courtesy of http://www.techlifeforum.net
ARGV has a few different meanings depending on how it's defined and used, but for this tutorial we will say that it means command line arguments for when the script is told to be interpreted.
For example, what I mean is that these would be ARGV values: <*colored in blue*>
Quote:File.pl ARG1 ARG2 ARG3
We can now split each like we do in an array to use the value of each as a variable and use if statements in perl to make sure that all of the arguments are filled.
To start, we will use an if statement to define an output if there are no arguments. An example of this would be if someone tried to interpret a perl script normally like this:
Quote:script.pl
Code:
if ($#ARGV < 0) {
print q{
=============================================================
| |
| Coded by Ace - 2011 |
| http://www.techlifeforum.net |
| |
|-----------------------------------------------------------|
| Syntax - file.pl ARG1 ARG2 ARG3 |
| |
=============================================================
};
exit;
}
I usually print out a little title menu like this to show the person trying to run my perl script that you need to use a certain syntax for it to work, in this case something like:
Quote:file.pl ARG1 ARG2 ARG3
If there are no arguments:
Quote:file.pl
The title menu will show up because of this if statement here:
Code:
if ($#ARGV < 0) {
}
And nothing else in the script will be read because we told it to exit after the "menu" is displayed:
Code:
exit;
Now we can go on to define what to respond when not enough arguments have been given a value. So if we are looking for 3 arguments, and the person uses this command:
Quote:file.pl ARG1 ARG2
Only 2 arguments have been defined for us to use, and that's no good. Therefor we can add onto our if statement with something like this to show that the person has not specified enough arguments like the syntax told them to do:
Code:
elsif ($#ARGV < 2) {
print "You have not provided all of the arguments required\n";
exit;
}
This tells the script to show/"print out" a response ("You have not provided all of the arguments required") if 3 arguments have not been given a value in the first command input (ARG 1 = 0, ARG 2 = 1, ARG 3 = 2). 0 is a placeholder for the first argument, therefor 0-2 is actually 3 arguments. So in our script, when we say $#ARGV < 2, we actually mean if it's less than 3 arguments. Then we exit with "exit;" like we did in the last example so that none of the rest of the script can be read.
If everything is good, and the user enters the syntax properly, now we can use those values and turn them into variables:
Code:
$arg1 = $ARGV[0];
$arg2 = $ARGV[1];
$arg3 = $ARGV[2];
From this command input, think of it this way; blue = first argument ($ARGV[0]), green = second argument ($ARGV[1]), and orange = third argument ($ARGV[2]):
Quote:file.pl first second third
To show those variables that we've given values to our arguments, now we can simply just print them out:
Code:
print "\n Output:\n---------\n";
print "\n> First Argument/Parameter: $arg1\n\n";
print "> Second Argument/Parameter: $arg2\n\n";
print "> Third Argument/Parameter: $arg3\n\n";
*Note that \n is a special entry for a new line
Full Script:
Code:
#!usr/bin/perl
if ($#ARGV < 0) {
print q{
=============================================================
| |
| Coded by Ace - 2011 |
| http://www.techlifeforum.net |
| |
|-----------------------------------------------------------|
| Syntax - file.pl ARG1 ARG2 ARG3 |
| |
=============================================================
};
exit;
} elsif ($#ARGV < 2) {
print "You have not provided all of the arguments required\n";
exit;
}
$arg1 = $ARGV[0];
$arg2 = $ARGV[1];
$arg3 = $ARGV[2];
print "\n Output:\n---------\n";
print "\n> First Argument/Parameter: $arg1\n\n";
print "> Second Argument/Parameter: $arg2\n\n";
print "> Third Argument/Parameter: $arg3\n\n";
For a visual on how this script works, i've initiated all outcomes in this image here:
*Blue Box - We have entered something like this in the command console:
Quote:file.pl
*Purple Box - We have entered something like this in the command console: (Missing an argument/parameter in the syntax)
Quote:file.pl ARG1 ARG2
*Orange Box - We have entered something like this in the command console: (All arguments/parameters have been given values and the syntax has been correctly inputted)
Quote:file.pl ARG1 ARG2 ARG3
I personally put this test script together for learning purposes. I hope you'll enjoy it.
Courtesy of http://www.techlifeforum.net