Thread Übergabe Hash-Struktur an Methode (33 answers)
Opened by mtbf40 at 2015-05-19 15:44

mtbf40
 2015-05-21 10:14
#181117 #181117
User since
2010-06-03
17 Artikel
BenutzerIn
[default_avatar]
so, habe jetzt eine Lösung gefunden die variabel alle Änderungen vornimmt
warum der <****>-Teil nicht funktioniert - erst einmal egal


Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/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";

Last edited: 2015-05-21 10:24:08 +0200 (CEST)

View full thread Übergabe Hash-Struktur an Methode