| 1 | package Mnet::Opts; | ||||||||||||
| 2 | |||||||||||||
| 3 | =head1 NAME | ||||||||||||
| 4 | |||||||||||||
| 5 | Mnet::Opts - Work with Mnet::Opts objects | ||||||||||||
| 6 | |||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||
| 8 | |||||||||||||
| 9 | # requried to use this module | ||||||||||||
| 10 | use Mnet::Opts; | ||||||||||||
| 11 | |||||||||||||
| 12 | # some options can be set as pragmas | ||||||||||||
| 13 | use Mnet::Opts::Set::Debug; | ||||||||||||
| 14 | |||||||||||||
| 15 | # options objects can be created | ||||||||||||
| 16 | my $opts = Mnet::Opts({ default => 1 }); | ||||||||||||
| 17 | |||||||||||||
| 18 | # options can be accessed via hash keys | ||||||||||||
| 19 | my $value = $opts->{default}; | ||||||||||||
| 20 | |||||||||||||
| 21 | # options can be accessed via method call | ||||||||||||
| 22 | $value = $opts->default; | ||||||||||||
| 23 | |||||||||||||
| 24 | =head1 DESCRIPTION | ||||||||||||
| 25 | |||||||||||||
| 26 | Mnet::Opts can be used to work with new Mnet::Opts objects, as shown in the | ||||||||||||
| 27 | example above. | ||||||||||||
| 28 | |||||||||||||
| 29 | Refer also to L<Mnet::Opts::Cli> module, used for parsing command line options. | ||||||||||||
| 30 | |||||||||||||
| 31 | =head1 METHODS | ||||||||||||
| 32 | |||||||||||||
| 33 | Mnet::Opts implements the methods listed below. | ||||||||||||
| 34 | |||||||||||||
| 35 | =cut | ||||||||||||
| 36 | |||||||||||||
| 37 | # required modules | ||||||||||||
| 38 | 168 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 39 | 168 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 40 | 168 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 41 | 168 | ▶ | use Mnet; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 42 | 168 | ▶ | use Mnet::Dump; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 43 | 168 | ▶ | use Mnet::Log::Conditional qw( DEBUG INFO WARN FATAL NOTICE ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 44 | 168 | ▶ | use Mnet::Opts::Set; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 45 | 168 | ▶ | use Storable; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 46 | |||||||||||||
| 47 | # declare global var for autoload | ||||||||||||
| 48 | 168 | our $AUTOLOAD; | |||||||||||
| 49 | |||||||||||||
| 50 | |||||||||||||
| 51 | |||||||||||||
| 52 | 4 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=7 SCAR=19.7
Pod
documented
| |||||||||||||
| 53 | |||||||||||||
| 54 | =head2 new | ||||||||||||
| 55 | |||||||||||||
| 56 | $opts = Mnet::Opts->new(\%opts) | ||||||||||||
| 57 | |||||||||||||
| 58 | The Mnet::Opts->new class method returns an Mnet::Opts option object. | ||||||||||||
| 59 | |||||||||||||
| 60 | The input opts hash reference argument is not required. Any | ||||||||||||
| 61 | An input hash reference of options can be supplied but is not required. | ||||||||||||
| 62 | |||||||||||||
| 63 | Note that any L<Mnet::Opts::Set> sub-modules that have been used will be set in | ||||||||||||
| 64 | the output Mnet::Opts object. | ||||||||||||
| 65 | |||||||||||||
| 66 | =cut | ||||||||||||
| 67 | |||||||||||||
| 68 | # read input class and optional opts hash ref | ||||||||||||
| 69 | 4 | ▶ | my $class = shift // croak("missing class arg"); | ||||||||||
|
Condition50%1/2
shift() // croak("missing class arg")
MC/DC0%0/1
shift() // croak("missing class arg")
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||
| 70 | 4 | ▶ | my $opts = shift // {}; | ||||||||||
|
Condition100%2/2
shift() // {}
MC/DC100%1/1
shift() // {}
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||
| 71 | |||||||||||||
| 72 | # create new object from input opts hash ref | ||||||||||||
| 73 | # dclone is used to avoid worries if caller edits hash values | ||||||||||||
| 74 | 4 | my $self = bless(Storable::dclone($opts), $class); | |||||||||||
| 75 | |||||||||||||
| 76 | # apply opts set via pragma use commands | ||||||||||||
| 77 | # these opts are not applied if already set in input | ||||||||||||
| 78 | 4 | my $pragmas = Mnet::Opts::Set::pragmas(); | |||||||||||
| 79 | 4 | foreach my $opt (keys %$pragmas) { | |||||||||||
| 80 | 4 | ▶ | next if exists $self->{$opt}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 81 | 4 | $self->{$opt} = $pragmas->{$opt}; | |||||||||||
| 82 | } | ||||||||||||
| 83 | |||||||||||||
| 84 | # log options, including who called us | ||||||||||||
| 85 | 4 | my $log = Mnet::Log::Conditional->new($opts); | |||||||||||
| 86 | 4 | ▶ | $log->debug("no opts") if not keys %$self; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 87 | 4 | $log->debug("$_ = ".Mnet::Dump::line($self->{$_})) foreach sort keys %$self; | |||||||||||
| 88 | |||||||||||||
| 89 | # finished new method, return Mnet::Opts object | ||||||||||||
| 90 | 4 | return $self; | |||||||||||
| 91 | } | ||||||||||||
| 92 | |||||||||||||
| 93 | |||||||||||||
| 94 | |||||||||||||
| 95 | 205 | ▶ | sub AUTOLOAD { | ||||||||||
|
Subroutine
AUTOLOAD: called CC=4 SCAR=13.9
| |||||||||||||
| 96 | |||||||||||||
| 97 | =head2 option methods | ||||||||||||
| 98 | |||||||||||||
| 99 | $value = $opts->$option | ||||||||||||
| 100 | |||||||||||||
| 101 | Option values may be accessed using autoloaded method calls, for example: | ||||||||||||
| 102 | |||||||||||||
| 103 | use Mnet::Opts; | ||||||||||||
| 104 | my $opts = Mnet::Opts({ default => 1 }); | ||||||||||||
| 105 | my $value = $opts->default; | ||||||||||||
| 106 | |||||||||||||
| 107 | Note that the universal 'can' method call does not work for these autoloaded | ||||||||||||
| 108 | option name methods. Method calls for options that do not exist will return | ||||||||||||
| 109 | a value of undefined. | ||||||||||||
| 110 | |||||||||||||
| 111 | It is also ok to directly access the values of hash keys in the options object. | ||||||||||||
| 112 | |||||||||||||
| 113 | =cut | ||||||||||||
| 114 | |||||||||||||
| 115 | # skip global destruction, return value for options that exist or undef | ||||||||||||
| 116 | 205 | my $self = shift; | |||||||||||
| 117 | 205 | ▶ | return if $AUTOLOAD =~ /::DESTROY$/; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 118 | 51 | ▶ | return if $AUTOLOAD !~ /::([^:]+)$/; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 119 | 51 | ▶ | return $self->{$1} if exists $self->{$1}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 120 | 1 | return undef; | |||||||||||
| 121 | } | ||||||||||||
| 122 | |||||||||||||
| 123 | |||||||||||||
| 124 | |||||||||||||
| 125 | =head1 SEE ALSO | ||||||||||||
| 126 | |||||||||||||
| 127 | L<Mnet> | ||||||||||||
| 128 | |||||||||||||
| 129 | L<Mnet::Opts::Cli> | ||||||||||||
| 130 | |||||||||||||
| 131 | L<Mnet::Opts::Set> | ||||||||||||
| 132 | |||||||||||||
| 133 | =cut | ||||||||||||
| 134 | |||||||||||||
| 135 | # normal package return | ||||||||||||
| 136 | 168 | 1; | |||||||||||
| 137 | |||||||||||||