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

Difference between revisions of "Perl/Basics/User Input/Command Line/Getopt::Std/Analysis"

From NetSec
Jump to: navigation, search
(Created page with "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 ...")
 
(No difference)

Latest revision as of 03:12, 16 July 2012

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.