#!/usr/bin/perl use strict; use warnings; use YAML::Tiny; use Data::Dumper; my $string = do{ local $/; }; 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