#!/usr/bin/perl use warnings; use strict; package childmodul; sub new { my ($classname,$hsh_scriptParam) = @_; my $self = {%{$hsh_scriptParam}}; return bless($self, $classname); } sub set_Data { my( $self, $val, @keys ) = @_ ; # das Objekt als Referenz übergeben # man könnte auch das Objekt selber mit bless gleich referenzieren # das macht aber die weitere Arbeit mit dem Objekt schwierig, da es # dann immer derefernziert werden müsste my $ref_ref = \$self; unless ( @keys ) { warn "deep_hash_assign: no keys" ; return ; } foreach my $key ( @keys ) { my $ref = ${$ref_ref} ; # this is the autoviv step unless ( defined( $ref ) ) { $ref = { $key => undef } ; ${$ref_ref} = $ref ; } # this checks we have a valid hash ref as a current value #**** das funktioniert bei einer Methode nicht??? **** #unless ( ref $ref eq 'HASH' and exists( $ref->{ $key } ) ) { # # warn "deep_hash_assign: not a hash ref at $key in @keys" ; # return ; #} # this points to the next level down the hash tree $ref_ref = \$ref->{ $key } ; } ${$ref_ref} = $val; } package main; my $hsh_runtimeParam = { ScriptParam => {ScriptName => $0 || '???', Version => 'v.01' || '???', Hilfe => {Usage => 'ruf mich mal richtig auf', Message => 'rufe mal nach Mutti' }, }, Runtime => {ApplName => 'TestClass', Caller => $^O =~ /win32/i ? $ENV{USERNAME} : $ENV{USER}, Loglevel => 'höher geht nicht', RootPath => 'ganz oben' || '???', StartTime => 'am Anfang' || '???', }, DBParam => {RegPar => 'test', dbSession => {ApplName => 'testhash', Action => 'insert', runtime => {dbTable => 'INIT_REG'}} } }; my $Object1 = childmodul->new($hsh_runtimeParam); $Object1->set_Data('INIT_RUN', qw( DBParam dbSession runtime dbTable )); print $Object1->{DBParam}->{dbSession}->{runtime}->{dbTable} . "\n"; $Object1->set_Data('update', qw( DBParam dbSession Action )); print $Object1->{DBParam}->{dbSession}->{Action} . "\n"; $Object1->set_Data('debug', qw( Runtime Loglevel )); print $Object1->{Runtime}->{Loglevel} . "\n";