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

Linuxer
 2015-05-22 01:41
#181130 #181130
User since
2006-01-27
3870 Artikel
HausmeisterIn

user image
Ja, das String-eval() meinte ich. Ich glaub, in der vorigen Version des editierten Absatzes stand das noch drin und ist beim Update dann rausgefallen.


Wenn ich das richtig sehe, könnte man das ohne eval() beispielsweise so lösen, indem man über die gegebene Liste von Hash-Keys iteriert und jede einzelne Ebene in einer Hilfsvariable vorhält und diese dann nutzt, um für den letzten Key den gegebenen Value einzutragen.

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
#!/usr/bin/perl

use warnings;
use strict;


package childmodul;

    sub new {    
        my $classname = shift;
        my $self = {};
        $self->{ScriptParam}->{ScriptName} = $0;
        $self->{ScriptParam}->{Version} = "v.01";
        $self->{ScriptParam}->{Hilfe}->{Usage} = "ruf mich mal richtig auf";
        $self->{ScriptParam}->{Hilfe}->{Message} = 'rufe mal nach Mutti';
        $self->{Runtime}->{ApplName} = "TestClass";
        $self->{Runtime}->{Caller} = $^O =~ /win32/i ? $ENV{USERNAME} : $ENV{USER};
        $self->{Runtime}->{Loglevel} = "höher geht nicht";
        $self->{Runtime}->{RootPath} = "ganz oben";
        $self->{Runtime}->{StartTime} = "am Anfang";
        $self->{DBParam}->{RegPar} = "test";
        $self->{DBParam}->{dbSession}->{ApplName} = "testhash";
        $self->{DBParam}->{dbSession}->{Action} = "insert";
        $self->{DBParam}->{dbSession}->{runtime}->{dbTable} = "INIT_REG";
        return bless($self, $classname);
    }

    sub set_Data {
        my $self = shift;
        my $val  = shift;
        my @locations = @_;
        
        # save last key
        my $last_location = pop @locations;

        # helper ref for each level inside HoHoH...
        my $ref = $self;
        for my $location ( @locations ) {
                # create new hash reference at this level if not already there
                $ref->{$location} = {}  if !exists $ref->{$location};

                # save reference to current level in helper ref
                $ref = $ref->{$location};
        }

        # save value in last key
        $ref->{$last_location} = $val;
    }

package main;

my $Object1 = childmodul->new();
$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";
meine Beiträge: I.d.R. alle Angaben ohne Gewähr und auf Linux abgestimmt!
Die Sprache heisst Perl, nicht PERL. - Bitte Crossposts als solche kenntlich machen!

View full thread Übergabe Hash-Struktur an Methode