The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


Getopt::Long::Spec - translate Getopt::Long specs into a hash of attributes, and back again


version 0.002


  use Getopt::Long::Spec;

  my $gls = Getopt::Long::Spec->new;

  my %attrs = $gls->parse('foo|f=i@{3,4}');

  my $spec  = $gls->build(
    long         => 'foo',
    short        => 'f',
    val_required => 1,
    val_type     => 'int',
    dest_type    => 'array',
    min_vals     => 3,
    max_vals     => 4,


This dist provides a means of parsing Getopt::Long's option specifications and turning them into hashes describing the spec. Furthermore, it can do the inverse, turning a hash into an option spec!

Care has been taken to ensure that the output of "parse" can always be fed back into "build" to get the exact same spec - essentially round-tripping the spec passed to parse.

I'm not yet sure it works the other way arround, that the hashes are round-tripped, but I am less concerned with that for no other reason than the fact that the code is already twisted enough as it is to ensure the former situation. :)



Simple constructor, takes no options.


Given a valid Getopt::Long option specification, this method returns a hash describing the spec (see synopsis, above).

This can be called as an object or class method. It will throw an exception on any error parsing the spec.


Given a hash describing the attributes of a Getopt::Long option spec, builds and returns the spec described.

This can be called as an object or class method. It will throw an exception on any error interpreting the attributes in the hash, for example, if the attributes conflict with each other, or if they would result in building an option spec that GoL would reject.

The attributes that may be used are as follows:


At some point I decided I wanted to create the ultimate command-line option processor. However, there are already so many out there, that I also wanted to be compatible with the most used of them. Since Getopt::Long is pretty much the de-facto standard in option processors, I realized, I need to make whatever I build able to use GoL's option specifications... hence this module.


Stephen R. Scaffidi <>


This software is copyright (c) 2012 by Stephen R. Scaffidi.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.