tags:

views:

74

answers:

2

I have this YAML file:

name: Firas
dirs: [/bin/, /home/phiras/]

I am using YAML::Syck in perl to parse this file, and I have a problem with accessing dirs items. my code is:

#!/usr/local/bin/perl

use strict;
use warnings;
use YAML::Syck;
use ConfigLoader;
use Data::Dumper;

my $conf = LoadFile("myconf.yml") || die("Error: Open config file \n");

print $conf->{name}, "\n";

my @dirs = $conf->{dirs};

print Dumper @dirs;
foreach(@dirs){
        print "$_\n";
}

the output is :

    Firas
    $VAR1 = [
              '/bin/',
              '/home/phiras/'
            ];
    ARRAY(0x8470d6c)

as you can see the loop is printing one item and it is considered as array. am I doing it in the right way?

+15  A: 

I think the problem is that $conf->{dirs} is an arrayref, not an array. Try this:

my @dirs = @{$conf->{dirs}};
zoul
Yes you are right.
Firas
@Firas - If someone gives you the answer, you should click on the checkmark next to the question to accept their answer as correct.
Chris Lutz
+1  A: 

I like to use the Dump class method to Data::Dumper. You give it two anonymous arrays. The first is a list of things to dump, and the second is the list of names to give the variables instead of $VARn. If you precede the name with a *, Dump figures out the data type and writes it as that type instead of a reference:

 print Data::Dumper->Dump( [ \@dirs ], [ qw(*dirs) ] );

The output more accurately represents what you have since you don't have to do the mental mapping back to @dirs. Now it's easy to see that you have a one element array:

@dirs = (
    [
   '/bin/',
   '/home/phiras/'
    ]
  );
brian d foy