Questions about this topic? Sign up to ask in the talk tab.

Difference between revisions of "Perl/Basics/User Input/Command Line"

From NetSec
Jump to: navigation, search
(Created page with "Command line arguments are passed at execution time; e.g. perl script.pl -a arg1 -b arg2 ...")
 
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
 
Command line arguments are passed at execution time; e.g.  
 
Command line arguments are passed at execution time; e.g.  
 
   perl script.pl -a arg1 -b arg2 ...
 
   perl script.pl -a arg1 -b arg2 ...
 +
 +
====Getopt::Std====
 +
{{:Perl/Basics/User_Input/Command_Line/Getopt::Std}}
 +
====Getopt::Long====
 +
{{:Perl/Basics/User_Input/Command_Line/Getopt::Long}}

Latest revision as of 01:39, 19 July 2012

Command line arguments are passed at execution time; e.g.

 perl script.pl -a arg1 -b arg2 ...

Getopt::Std

This requires Getopt::Std. The perldoc is here.

Code
 
use strict;
use warnings;
use Getopt::Std;
 
my %opts;
getopts('m:b',\%opts);
 
print $opts{m} . "\n";
print "The boolean -b option was set!\n" if defined $opts{b};
print "The boolean -b option was not set!\n" if undef $opts{b};
 
Analysis

The getopts() function takes a string of flags to parse as well as a hash reference. You can execute the script as follows:

 perl script.pl -m "hello" -b
 perl script.pl -m "hello"

In the above example, we see the line:

 getopts('m:b',\%opts);

The 'm:b', the first argument to the function, designates what command line arguments to parse. The colon after the 'm' specifies that it takes an additional parameter, in this case, the message to say. The -b does not have a colon; we are using it to demonstrate a flag that does not require an additional parameter.

The second argument is a hash reference to designate where the return data is stored; in this case, $opts{m} contains "hello" and opts{b} is either defined or undefined based on whether or not it was present in the flags when the script was executed.

Getopt::Long

This requires Getopt::Long. The perldoc is here.

Code
 
use strict;
use warnings;
use Getopt::Long;
 
my $message, $boolean;
GetOptions('message=s' => \$message, 'boolean' => \$boolean);
 
print $message . "\n";
print "The boolean -b option was set!\n" if defined $boolean;
print "The boolean -b option was not set!\n" if undef $boolean;
 
Analysis

The GetOptions() function receives message formats and references for variable assignment. You can execute the script as follows:

 perl script.pl --message "hello" --boolean
 perl script.pl --message "hello"

In the above example, we see the line:

 GetOptions('message=s' => \$message, 'boolean' => \$boolean);

You can see from the execution pattern above that the GetOptions() function provides an interface for the "double-dash" style command line arguments. The GetOptions() function receives a hash. The =s after message designates that the --message parameter receives a string data type. An =i will change it to integer. Simple no = will set the flag to a boolean; similar to an argument without a colon in Getopt::Std. Notice each variable is passed as a reference.