#!/usr/bin/perl use strict; use warnings; use YAML::Syck; my $hash = { 'k1'=>'v1', 'k2'=>'v2', 'k3'=>'v3', }; # Part 1 - works fine while ( my ($k, $v) = each(%$hash) ) { print "$k, $v\n"; } # Part 3 - use foreach loop instead of while(each) foreach my $k (keys %$hash) { print "$k, $hash->{$k}\n"; DumpFile("test.yml", $hash); } __END__ stdout prints: k3, v3 k2, v2 k1, v1 k3, v3 k2, v2 k1, v1 test.yml file has: --- k1: v1 k2: v2 k3: v3