perlyaml

Extract data from YAML file


I need to pick up values associated with the variables from a YAML file using Perl.

I have tried this so far.

my $fh = new FileHandle; 
my $mydir = "/proj/1a/ct_tst/cf";
my $file  = "tsan.yml";
opendir($dh, $mydir) or die ("Cannot read directory!\n");
open($fh, "< ${mydir}/${file}") or die ("Cannot read file $file!\n");

It works to load a file. But, I am not sure how to proceed to get the values corresponding to variables in the YAML file.


Solution

  • You should use a YAML parser (like YAML.pm).

    use YAML;
    
    my $data = LoadFile("$mydir/$myfile");
    

    Now, in $data, you will have a Perl data structure containing all of the data from the YAML file. But as I don't know what your YAML looks like, I can't help you extract specific data from it.