in reply to yaml dynamically load in perl

Is it doable?

Yes, of course. How would you do it? Do you have some thoughts about how this might be done? Code?

I think that this tasks amounts to having config values treated as macros. A distinction between literal values, macros and referenced YAML keys has to be established. Macro values would be turned into an anonymous subs, so retrieving a value would be done like this (shown here with a hash):

my $val = ref $yaml{$key} eq 'CODE' ? $yaml{$key}->() : $yaml{$key};

or the like. You'd be inventing a macro language for your config files. The node tied hash for data munging is based on that. You should be very careful if you are going to shell out to get a value, since typos or mischief can be disastrous. Always run such code under -T (taint checks - see perlrun)!

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

Replies are listed 'Best First'.
Re^2: yaml dynamically load in perl
by louie_45 (Acolyte) on Nov 02, 2015 at 07:04 UTC

    Thanks a lot for your inputs. Would check tied hash later.

    And could you show my how to call the 'CODE' or 'ref' in Yaml.

    For instance, Here is Yaml format: cmd:!perl/ref my_print

    Perl Code:
    $my_print = sub { print "howdy"; };
    How do I make 'cmd' to call $my_print after load the Yaml file into $conf like:
    &{ $conf->{"cmd"} }
    To make it run and print "howdy".

      Not sure if this answers your general question, but here are some specific alternatives:

      c:\@Work\Perl\monks>perl -wMstrict -le "my $my_print = sub { print qq{howdy @_}; }; ;; my $conf = { 'cmd' => $my_print }; ;; &{ $conf->{'cmd'} }; $conf->{'cmd'}->('there'); $conf->{cmd}('doody'); " howdy howdy there howdy doody


      Give a man a fish:  <%-{-{-{-<

        The point is that we need work with Yaml format:
        cmd:!perl/ref my_print
        Not directly with perl anonymous subroutine.