getopt-long-sample-implicit-dest-classic - Example of Getopt::Long usage with some implicit destinations
This document describes version 0.001 of getopt-long-sample-implicit-dest-classic (from Perl distribution App-GetoptLongExamples), released on 2019-01-30.
When getting options with Getopt::Long's GetOptions, one usually specifies a destination for each option specification:
GetOptions( 'foo=s' => \$foo, 'bar=s' => \@bar, 'help' => sub { ... }, );
But Getopt::Long allows not specifying these destinations. When the first argument is a reference to a hash ("hash storage mode") then the hash (with the option name as the key being looked up) will be consulted for the destination and if no such destination exist, the hash key will be set:
my %opts = ( bar => \@bar, ); GetOptions( \%opts, 'foo=s', # will set $opts{foo} 'bar=s', # will push to @bar 'help' => sub { ... }, );
When the first argument is not a hashref ("classic mode"), the corresponding $opt_OPTIONNAME will be set, without regard of destination types.
$opt_OPTIONNAME
GetOptions( 'foo=s', # will set $opt_foo to option value, regardless of current content of $opt_foo 'bar=s', # will set $opt_bar to option value, regardless of current content of $opt_bar 'help' => sub { ... }, );
Please visit the project's homepage at https://metacpan.org/release/App-GetoptLongExamples.
Source repository is at https://github.com/perlancar/perl-App-GetoptLongExamples.
Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=App-GetoptLongExamples
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.
Getopt::Long
perlancar <perlancar@cpan.org>
This software is copyright (c) 2019 by perlancar@cpan.org.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install App::GetoptLongExamples, copy and paste the appropriate command in to your terminal.
cpanm
cpanm App::GetoptLongExamples
CPAN shell
perl -MCPAN -e shell install App::GetoptLongExamples
For more information on module installation, please visit the detailed CPAN module installation guide.