Commit 015b2271 authored by Bernard Quatermass's avatar Bernard Quatermass

Allow for non-hashref defaults

parent 37a7c3a0
......@@ -4,7 +4,7 @@
# VERSION
version 1.00
version 2.0
# SYNOPSIS
......@@ -54,6 +54,11 @@ composite hash using each key that has not already been defined.
This result is then passed to **Class::Accessor** as the initial
hash.
To aid in integration with existing code you can also supply the
initialising data in normal hash list format (or even a mix).
> my $ifile = InterestingFile->new( 'filename' => 'something.real', $inherited_hash_ref );
# COMPATIBILITY
- **Class::Accessorize** requires **Class::Accessor** (quelle surprise).
......@@ -64,7 +69,7 @@ Bernard Quatermass <toolsmith@quatermass.co.uk>
# COPYRIGHT AND LICENSE
This software is copyright (c) 2017 by Bernard Quatermass.
This software is copyright (c) 2017, 2018 by Bernard Quatermass.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
Name: perl-Class-Accessorize
Version: 1.1
Version: 2.0
Release: 1%{?dist}
Summary: defaults wrapper around Class::Accessor
......@@ -52,6 +52,9 @@ make test
%changelog
* Mon Jan 27 2020 Bernard Quatermass <toolsmith@quatermass.co.uk>> - 2.0-1
- Allow for non-hashref defaults
* Tue Oct 2 2018 Bernard Quatermass <toolsmith@quatermass.co.uk>> - 1.1-1
- Update for git-build-rpm
......
......@@ -5,7 +5,7 @@ use strict;
package Class::Accessorize;
$Class::Accessorize::VERSION='1.1';
$Class::Accessorize::VERSION='2.0';
use Class::Accessor;
use base qw(Class::Accessor);
......@@ -27,16 +27,31 @@ new
my $self = shift || {};
# Overlay all undefined keys
foreach my $deflayer( @_ )
# V2.0 - get flexible and allow for non-hash-ref defaults.
# on the observation that if the first item is not a hash ref
# then it must be a hash key(scalar) followed by the value for the key
# So we either transfer the hash contents, or take a key/value pair
# off the list and set accordingly.
while( $k = shift @_ )
{
# Handle undef in @_ list
next if ! $deflayer;
foreach( keys %$deflayer )
{
$self->{$_} = $deflayer->{$_}
if ! exists $self->{$_};
}
}
# Handle undef in @_ list
next if ! $k;
if (ref($k))
{
foreach( keys %$k )
{
$self->{$_} = $k->{$_}
if ! exists $self->{$_};
}
}
else
{
$self->{$k} = shift @_
if @_;
}
};
return bless $self, $class;
}
......@@ -50,7 +65,7 @@ B<Class::Accessorize> - Wrapper for B<Class::Accessor> to allow defaults
=head1 VERSION
version 1.1
version 2.0
=head1 SYNOPSIS
......@@ -61,11 +76,11 @@ version 1.1
sub
new
{
my $protoverse = shift;
my $class = ref($protoverse) || $protoverse;
my $self = $class->SUPER::new( shift || {}, @_,
{ 'filename' => '/dev/null' } );
return $self;
my $protoverse = shift;
my $class = ref($protoverse) || $protoverse;
my $self = $class->SUPER::new( shift || {}, @_,
{ 'filename' => '/dev/null' } );
return $self;
}
package InterestingFile
......@@ -76,11 +91,11 @@ version 1.1
sub
new
{
my $protoverse = shift;
my $class = ref($protoverse) || $protoverse;
my $self = $class->SUPER::new( shift || {}, @_,
{ 'filename' => '/dev/random' } );
return $self;
my $protoverse = shift;
my $class = ref($protoverse) || $protoverse;
my $self = $class->SUPER::new( shift || {}, @_,
{ 'filename' => '/dev/random' } );
return $self;
}
my $ifile = InterestingFile->new( { 'filename' => 'something.real' } );
......@@ -100,6 +115,15 @@ composite hash using each key that has not already been defined.
This result is then passed to B<Class::Accessor> as the initial
hash.
To aid in integration with existing code you can also supply the
initialising data in normal hash list format (or even a mix).
=over 4
my $ifile = InterestingFile->new( 'filename' => 'something.real', $inherited_hash_ref );
=back
=head1 COMPATIBILITY
=over 4
......@@ -116,14 +140,14 @@ Bernard Quatermass <toolsmith@quatermass.co.uk>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2017, 2018 by Bernard Quatermass.
This software is copyright (c) 2017, 2018, 2020 by Bernard Quatermass.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
# vi: aw ai sw=4
# vi: aw ai sw=4 et
###############################################################
## End of File
###############################################################
###############################################################
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment