The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Data::Startup - startup options class, override, config methods

SYNOPSIS

 ######
 # Subroutine interface
 #
 use Data::Startup qw(config override);
 
 $options = override(\%default_options, @option_list );
 $options = override(\%default_options, \@option_list );
 $options = override(\%default_options, \%option_list );

 @options_list = config(\%options );

 ($key, $old_value) = config(\%options, $key);
 ($key, $old_value) = config(\%options, $key => $new_value ); 
 ($key, $old_value) = config(\%options, $key => $new_value );

 @old_options_list = config(\%options, @option_list);
 @old_options_list = config(\%options, \@option_list);
 @old_options_list = config(\%options, \%option_list);

 ######
 # Object interface
 #
 use Data::Startup

 $startup_options = $class->Data::Startup::new( @option_list );
 $startup_options = $class->Data::Startup::new( \@option_list );
 $startup_options = $class->Data::Startup::new( \%option_list );

 $options = $startup_options->override( @option_list );
 $options = $startup_options->override( \@option_list );
 $options = $startup_options->override( \%option_list );

 @options_list = $options->config( );

 ($key, $old_value) = $options->config($key);
 ($key, $old_value) = $options->config($key => $new_value ); 
 ($key, $old_value) = $options->config($key => $new_value );

 @old_options_list = $options->config(@option_list);
 @old_options_list = $options->config(\@option_list);
 @old_options_list = $options->config(\%option_list);

 # Note: May use [@option_list] instead of \@option_list
 #       and {@option_list} instead of \%option_list

DESCRIPTION

Many times there is a group of subroutines that can be tailored by different situations with a few, say global variables. However, global variables pollute namespaces, become mangled when the functions are multi-threaded and probably have many other faults that it is not worth the time discovering.

As well documented in literature, object oriented programming do not have these faults. This program module class of objects provide the objectized options for a group of subroutines or encapsulated options by using the methods directly as in an option object.

The Data::Startup class provides a way to input options in very liberal manner of either

  • arrays, reference to an array, or reference to hash to a

  • reference to an array or reference to a hash

  • reference to a hash

  • referene to an array

  • many other combos

without having to cut and paste specialize, tailored code into each subroutine/method.

Some of the possiblities follows.

A subroutine may be utilize either as a subroutine or a method of a object by processing the first argument of @_ by the following:

 sub my_suroutine
 {
     shift if UNIVERSAL::isa($_[0],__PACKAGE__);

     # ....

 }

The Data::Startup class may be used to provide various options syntax for a dual methods/subroutines as follows:

 my $default_options = new( @default_options_list);

 # SYNTAX: my_subroutine1($arg1 .. $argn, @options)
 #         my_subroutine1($arg1 .. $argn, \@options)
 #         my_subroutine1($arg1 .. $argn, \%options)
 #
 

 sub my_subroutine1
 {
     shift if UNIVERSAL::isa($_[0],__PACKAGE__);
     $default_options = Data::Startup->new() unless $default_options;
     my ($arg1 .. $argn, @options) = @_
     my $options = $default_options->override(@options);

     # ....
 }

 # SYNTAX: my_subroutine2(\@options, @args)
 #         my_subroutine2(\%options, @args)
 #
 # !ref($args[0])

 sub my_subroutine2
 {
     shift if UNIVERSAL::isa($_[0],__PACKAGE__);
     $default_options = Data::Startup->new() unless $default_options;
     my $options = $default_options->override(shift @_) if ref($_[0]);

     # ....
 }

 # SYNTAX: my_subroutine3(\%options, @args)
 #
 # ref($args[0]) ne 'HASH'

 sub my_subroutine3
 {
     shift if UNIVERSAL::isa($_[0],__PACKAGE__);
     $default_options = Data::Startup->new() unless $default_options;
     my $options = $default_options->override(shift @_) if ref($_[0] eq 'HASH');
     my (@args) = @_;

     # ....
 }

If program module does not require program module wide global default options, than still use Data::Startup to provide liberal options syntax as follows

 # SYNTAX: my_subroutine1($arg1 .. $argn, @options)
 #         my_subroutine1($arg1 .. $argn, \@options)
 #         my_subroutine1($arg1 .. $argn, \%options)
 #
 
 sub my_subroutine4
 {
     shift if UNIVERSAL::isa($_[0],__PACKAGE__);
     my ($arg1 .. $argn, @options) = @_
     my $options = new Data::Startup(@options);

     # ....
 }

This technique may be extended to many more different subroutine with a similar style syntax.

The Data::Startup class may be used may also be used to create objects off a base $default_object as follows:

 use Data_Startup;
 unshift @ISA,'Data_Startup'; # first among classes
 use vars qw($default_object);
 $default_object = new Data::Startup( @default_list);

 sub new
 {
     $default_options->override( @_ );
 
 }  

 my $object = new my_package;
  
 my @old_options = object->config( @_ );
 my @old_default_options = $my_package::$default_object->config( @_ );

 sub method
 {
    $self = shift;
    $value1 = $self->{$key1};

 }
 

And then there are the hybrid subroutine, class syntax and probably some other possibilies that are not readily apparent.

METHODS

new

The new method c<bless> the input @option_list creating a default options hash object.

config

The config method reads and writes individual key,value pairs or groups of key,value pairs in the $option object.

The method response with no inputs with all the $key,$value pairs in $options; a single $key input with the $key,$value for that $key; and, a group of $key, $value pairs, @option_list by replacing all the $option $key in the group by the paired <$value> returning the @old_options_list of old $key,$value pairs. The config method does not care if the @option_list is an array, a reference to an array or a reference to a hash.

override

The override method takes a default options object, $startup_options, creates a new duplicate object, $options, keeping $startup_options intact, and replaces selected optioins in $options with override values, @option_list.

REQUIREMENTS

Coming.

DEMONSTRATION

 #########
 # perl Startup.d
 ###

~~~~~~ Demonstration overview ~~~~~

The results from executing the Perl Code follow on the next lines as comments. For example,

 2 + 2
 # 4

~~~~~~ The demonstration follows ~~~~~

     use File::Package;
     my $uut = 'Data::Startup';

     my ($result,@result); # provide scalar and array context
     my ($default_options,$options) = ('$default_options','$options');

 ##################
 # create a Data::Startup default options
 # 

 ($default_options = new $uut(
        perl_secs_numbers => 'multicell',
        type => 'ascii',   
        indent => '',
        'Data::SecsPack' => {}
    ))

 # bless( {
 #                 'perl_secs_numbers' => 'multicell',
 #                 'Data::SecsPack' => {},
 #                 'type' => 'ascii',
 #                 'indent' => ''
 #               }, 'Data::Startup' )
 #

 ##################
 # read perl_secs_numbers default option
 # 

 [$default_options->config('perl_secs_numbers')]

 # [
 #          'perl_secs_numbers',
 #          'multicell'
 #        ]
 #

 ##################
 # write perl_secs_numbers default option
 # 

 [$default_options->config(perl_secs_numbers => 'strict')]

 # [
 #          'perl_secs_numbers',
 #          'multicell'
 #        ]
 #

 ##################
 # restore perl_secs_numbers default option
 # 

 [$default_options->config(perl_secs_numbers => 'multicell')]

 # [
 #          'perl_secs_numbers',
 #          'strict'
 #        ]
 #

 ##################
 # create options copy of default options
 # 

 $options = $default_options->override(type => 'binary')

 # bless( {
 #                 'perl_secs_numbers' => 'multicell',
 #                 'Data::SecsPack' => {},
 #                 'type' => 'binary',
 #                 'indent' => ''
 #               }, 'Data::Startup' )
 #

 ##################
 # verify default options unchanged
 # 

 $default_options

 # bless( {
 #                 'perl_secs_numbers' => 'multicell',
 #                 'Data::SecsPack' => {},
 #                 'type' => 'ascii',
 #                 'indent' => ''
 #               }, 'Data::Startup' )
 #

 ##################
 # array reference option config
 # 

 [@result = $options->config([perl_secs_numbers => 'strict'])]

 # [
 #          'perl_secs_numbers',
 #          'multicell'
 #        ]
 #

 ##################
 # array reference option config
 # 

 $options

 # bless( {
 #                 'perl_secs_numbers' => 'strict',
 #                 'Data::SecsPack' => {},
 #                 'type' => 'binary',
 #                 'indent' => ''
 #               }, 'Data::Startup' )
 #

 ##################
 # hash reference option config
 # 

 [@result = $options->config({'Data::SecsPack'=> {decimal_fraction_digits => 30} })]

 # [
 #          'Data::SecsPack',
 #          {}
 #        ]
 #

 ##################
 # hash reference option config
 # 

 $options

 # bless( {
 #                 'perl_secs_numbers' => 'strict',
 #                 'Data::SecsPack' => {
 #                                       'decimal_fraction_digits' => 30
 #                                     },
 #                 'type' => 'binary',
 #                 'indent' => ''
 #               }, 'Data::Startup' )
 #

 ##################
 # verify default options still unchanged
 # 

 $default_options

 # bless( {
 #                 'perl_secs_numbers' => 'multicell',
 #                 'Data::SecsPack' => {},
 #                 'type' => 'ascii',
 #                 'indent' => ''
 #               }, 'Data::Startup' )
 #

 ##################
 # create a hash default options
 # 

   my %default_hash = (
        perl_secs_numbers => 'multicell',
        type => 'ascii',   
        indent => '',
        'Data::SecsPack' => {}
    );
 $default_options = \%default_hash

 # {
 #          'perl_secs_numbers' => 'multicell',
 #          'Data::SecsPack' => {},
 #          'type' => 'ascii',
 #          'indent' => ''
 #        }
 #

 ##################
 # override default_hash with an option array
 # 

 Data::Startup::override($default_options, type => 'binary')

 # {
 #          'perl_secs_numbers' => 'multicell',
 #          'Data::SecsPack' => {},
 #          'type' => 'binary',
 #          'indent' => ''
 #        }
 #

 ##################
 # override default_hash with a reference to a hash
 # 

 Data::Startup::override($default_options, {'Data::SecsPack'=> {decimal_fraction_digits => 30}})

 # {
 #          'perl_secs_numbers' => 'multicell',
 #          'Data::SecsPack' => {
 #                                'decimal_fraction_digits' => 30
 #                              },
 #          'type' => 'ascii',
 #          'indent' => ''
 #        }
 #

 ##################
 # override default_hash with a reference to an array
 # 

 Data::Startup::override($default_options, [perl_secs_numbers => 'strict'])

 # {
 #          'perl_secs_numbers' => 'strict',
 #          'Data::SecsPack' => {},
 #          'type' => 'ascii',
 #          'indent' => ''
 #        }
 #

 ##################
 # return from config default_hash with a reference to an array
 # 

 [@result = Data::Startup::config($default_options, [perl_secs_numbers => 'strict'])]

 # [
 #          'perl_secs_numbers',
 #          'multicell'
 #        ]
 #

 ##################
 # default_hash from config default_hash with a reference to an array
 # 

 $default_options

 # {
 #          'perl_secs_numbers' => 'strict',
 #          'Data::SecsPack' => {},
 #          'type' => 'ascii',
 #          'indent' => ''
 #        }
 #

QUALITY ASSURANCE

Running the test script Startup.t verifies the requirements for this module. The tmake.pl cover script for Test::STDmaker automatically generated the Startup.t test script, Startup.d demo script, and t::Data::Startup program module POD, from the t::Data::Startup program module contents. The tmake.pl cover script automatically ran the Startup.d demo script and inserted the results into the 'DEMONSTRATION' section above. The t::Data::Startup program module is in the distribution file Data-Startup-$VERSION.tar.gz.

NOTES

Author

The holder of the copyright and maintainer is

<support@SoftwareDiamonds.com>

Copyrighted (c) 2002 Software Diamonds

All Rights Reserved

Binding Requirements Notice

Binding requirements are indexed with the pharse 'shall[dd]' where dd is an unique number for each header section. This conforms to standard federal government practices, STD490A 3.2.3.6. In accordance with the License, Software Diamonds is not liable for any requirement, binding or otherwise.

License

Software Diamonds permits the redistribution and use in source and binary forms, with or without modification, provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

  3. Commercial installation of the binary or source must visually present to the installer the above copyright notice, this list of conditions intact, that the original source is available at http://softwarediamonds.com and provide means for the installer to actively accept the list of conditions; otherwise, a license fee must be paid to Softwareware Diamonds.

SOFTWARE DIAMONDS, http://www.softwarediamonds.com, PROVIDES THIS SOFTWARE 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTWARE DIAMONDS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING USE OF THIS SOFTWARE, EVEN IF ADVISED OF NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE POSSIBILITY OF SUCH DAMAGE.

SEE ALSO

Docs::Site_SVD::Data_Startup
Test::STDmaker