For scalars set the default in the call to GetOptions. However, for arrays, you'll need to be more explicit with your logic.
## Parse options and print usage if there is a syntax error,
## or if usage was explicitly requested.
GetOptions(
'help|?' => \(my $help = 0),
'man' => \(my $man = 0),
'complist:s@' => \my @compList,
'action:s@' => \my @actionList,
) or pod2usage(2);
# Defaults for array
@compList = qw(abc xyz) if !@compList;
@actionList = qw(clean build) if !@actionList;
Note, because $help
and $man
are just boolean flags, it's not actually necessary to initialize them. Relying on their default of undef
works fine unless you're trying to print their values out somewhere.