File: advanced.pl

package info (click to toggle)
libconfig-merge-perl 1.01-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 440 kB
  • ctags: 36
  • sloc: perl: 804; makefile: 2
file content (52 lines) | stat: -rw-r--r-- 1,404 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/perl
use strict;
use warnings;
use blib;
use Config::Merge();
use File::Spec();

eval        { require YAML::Syck; YAML::Syck->import(); 1 }
    or eval { require YAML;       YAML->import();       1 }
    or die "ERROR: "
    . "YAML::Syck or YAML needs to be installed to use this example\n\n";

my $debug = shift @ARGV;

foreach my $hostname qw(main dev1 dev2) {
    my $config = Config::Merge->new(
        path        => get_path('advanced'),
        debug       => $debug,

        # If matches 'xxx-(yyy)'
        is_local    => sub {
            my ($self,$name) = @_;
            return $name=~/- [(] .+ [)]/x ? 1 : 0;
        },

        # If local and matches 'xxx-(hostname)', return xxx
        load_as => sub {
            my ($self,$name,$is_local) = @_;
            if ($is_local) {
                if ($name=~/(.*) - [(] ($hostname) [)] /x) {
                    return  $1;
                }
                return undef;
            }
            return $name;
        }
    );
    print "\nCONFIG FOR $hostname:\n".Dump(scalar $config->C());
}

#===================================
sub get_path {
#===================================
    my ($vol,$path) = File::Spec->splitpath(
                   File::Spec->rel2abs($0)
            );
    $path = File::Spec->catdir(
        File::Spec->splitdir($path),
        ,@_
    );
    return File::Spec->catpath($vol,$path,'');
}