Thread Codegeneration aus XML Daten (7 answers)
Opened by Tr0Nix at 2008-01-07 20:30

renee
 2008-01-07 22:06
#104476 #104476
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Als Anfang:
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
#!/usr/bin/perl

use strict;
use warnings;
use YAML::Tiny;
use Data::Dumper;

my $string = do{ local $/; <DATA> };
my $config = YAML::Tiny->read_string( $string )->[0];

my $code = '';
variable_declaration( \$code, $config->{root} );

print $code;

sub variable_declaration{
    my ($coderef,$config) = @_;
    
    for my $name ( keys %$config ){
        if( ! ref( $config->{$name} ) ){
            declare_scalar( $coderef, $config->{$name}, $name );
        }
        elsif( ref($config->{$name}) eq 'HASH' ){
            declare_hash( $coderef, $config->{$name}, $name );
        }
        elsif( ref( $config->{$name} ) eq 'ARRAY' ){
            declare_array( $coderef, $config->{$name}, $name );
        }
    }
}

sub declare_hash{
    my ($coderef,$config,$name) = @_;
    
    $$coderef .= 'my %' . $name . " = (\n";
    
    for my $key ( keys %$config ){
        my $val = $config->{$key}->{value};
           $val = $config->{$key}->{type} eq 'qr' ? "qr/$val/" : $val;
        $$coderef  .= "$key => $val,\n"
    }

    $$coderef .= ");\n";
}

sub declare_array{
    my ($coderef,$config,$name) = @_;
    
    $$coderef .= "\nmy \@$name = (" . join(",", map{ '"' . $_ . '"' }@$config ) . ");\n";
}

__DATA__
---
root:
  regex:
    nameCheck:
      type: qr
      value: tr0nix
    nrCheck:
      type: qr
      value: ^\d$
    emptyCheck:
      type: qr
      value: ^$
  array :
    - 1
    - 2
code:
  startAt: 0
  check:
    - 0 :
      Action: return
      Regex: nameCheck
    - 1 :
      Check: 2
      Regex: nrCheck
    - 2 :
      Action: return
      Regex: emptyCheck
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/

View full thread Codegeneration aus XML Daten