# yam1.pl
use strict;
use warnings;
use YAML;
my $config = {
NAME => 'John Doe',
ADDRESS => '123 Main St.',
PHONE => {
'Home' => '123-4444',
'Work' => '123-5555',
},
CONTACTS => [
'John',
'Paul',
'George',
],
};
print Dump( $config ), "\n";
####
---
ADDRESS: 123 Main St.
CONTACTS:
- John
- Paul
- George
NAME: John Doe
PHONE:
Home: 123-4444
Work: 123-5555
####
# yam2.pl -- does NOT work!
use strict;
use warnings;
use YAML;
use Data::Dumper;
# load YAML file into perl hash ref?
my $config = Load("config.yml");
print Dumper($config), "\n";
####
# yam3.pl - Works! Yippie!
use strict;
use warnings;
use YAML;
use Data::Dumper;
# step 1: open file
open my $fh, '<', 'config.yml'
or die "can't open config file: $!";
# step 2: slurp file contents
my $yml = do { local $/; <$fh> };
# step 3: convert YAML 'stream' to perl hash ref
my $config = Load($yml);
print Dumper($config), "\n";
####
# yam4.pl - Works also! Yippie!
use strict;
use warnings;
use YAML;
use Data::Dumper;
# step 1: open file
open my $fh, '<', 'config.yml'
or die "can't open config file: $!";
# step 2: convert YAML file to perl hash ref
my $config = LoadFile($fh);
print Dumper($config), "\n";