How to create command line options in Perl

Home > Search > How-to

Let's say you want to create a Perl script and allow users to pass options to the script. For example, perhaps you want to let people pass their name and occuption to the script, like this.

perl --name John --occuption programmer


The following script will allow users to use the -n or --name and -o or --occupation options when running the script.

use Options;
my $options = new Options(params => [
  ['name',       'n', "", 'Name.'],
  ['occupation', 'o', "", 'Occupation.']
  flags =>  [
    ['quit',   'q', 'Quit after connecting.'],
    ['help',   'h', 'Display this usage guide.'],
my %argv = $options->get_options();
# Provide usage

my $name = lc($options->get_result('name'));
my $occupation= lc($options->get_result('occupation'));

print "$name\n";
print "$occupation\n";


Add a Comment

We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.

Please enter in the box below so that we can be sure you are a human.